从父页面隐藏表单,直到加载特定的iframe元素ID

时间:2013-04-12 20:52:52

标签: javascript jquery iframe

我只需要在iframe中加载特定元素ID时显示父页面表单。 parent / iframe位于同一个域中。如何使用javascript实现这一目标?

<form id="Search" action="../search.php" target="my-iframe" method="post">
<input type="text" id="location" name="keywords[all_words]"  />
<a href="#" onclick="document.getElementById('Search').submit()" >Search</a>
</form>

<iframe id="myiframe" src="../page.html" name="myiframe" width="100%" height="100%" scrolling="no" frameborder="0"></iframe>

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

// wait for the page to finish loading
window.onload = function(){

    // hide your form
    document.getElementById('Search').style.display = 'none';

    // get the document of the iframe
    var frameDocument = document.getElementById('myiframe').contentWindow.document;
    // if this is true the element with id 'test' is in the frame
    if(frameDocument.getElementById('test')){
        // show your form
        document.getElementById('Search').style.display = 'block';
    }
}

答案 1 :(得分:1)

您可以使用jQuery将load事件附加到iframe。这是一个有效的例子:http://jsfiddle.net/kPqbS/

相关问题