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

Javascript 从父页面隐藏表单,直到加载特定的iframe元素ID,javascript,jquery,iframe,Javascript,Jquery,Iframe,我只需要在iframe中加载特定元素ID时显示父页面表单。父/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.g

我只需要在iframe中加载特定元素ID时显示父页面表单。父/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>

您可以使用以下内容:

// 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';
    }
}

您可以使用jQuery将
load
事件附加到iframe。下面是一个工作示例:

我在控制台上得到语法错误(Null):var frameDocument=document.getElementById('myiframe').contentWindow.document;尽管iframe id是正确的。您可能需要将脚本代码包装在
window.onload=function(){…}
中。您可能会收到该错误,因为在scrip运行时尚未加载iframe。不客气,如果答案确实回答了您的问题,请记住接受答案。