读书人

关于iframe的小疑点

发布时间: 2012-09-25 09:55:58 作者: rapoo

关于iframe的小问题

HTML code
<table width="99%"  height="540px">                              <tr>                                <td  width="99%" align="left" valign="top"  style="border:1px solid #B4C9C6;height:180px;width:750px;background-color: #fff;">                                    <div style=" overflow:auto;height:100%;">                                        <s:form id="searchForm" target="myframe" action="searchSelectedQuestion" namespace="/data" onsubmit="return checkInput();">                                            <input type="submit" value="搜索" name="advencesearch" id="search-submit" style="margin-top: 10px"/>                                                                                        <table  class="t2"  id="showQuestion">                                                                                            </table>                                        </s:form>                                    </div>                                </td>                              </tr>                              <tr>                                <td style="border:1px solid #B4C9C6;height:340px;background-color: #fff;">                                    <iframe onload="initPage()" width="100%" height="100%" frameborder="0" id="myframe" name="myframe" scrolling="auto">                                                                            </iframe>                                </td>                              </tr>                        </table>


[解决办法]
parent.document.searchForm....
这样操作试过吗?
[解决办法]
首先楼主要把form表单里面的s标签删除,不然js不能识别该标签的,s应该是jsp里面的标签吧,以下是我写的方法,分别用js和jquery实现,希望对楼主有所帮助。
1、你先删除s标签,修改为
HTML code
<form id="searchForm" target="myframe" action="searchSelectedQuestion" namespace="/data" onsubmit="return checkInput();">                                            <input type="submit" value="搜索" name="advencesearch" id="search-submit" style="margin-top: 10px"/>                                                                                        <table  class="t2"  id="showQuestion">                                                                                            </table>                                        </form>
[解决办法]
另外附上父窗口与iframe子窗口之间传值的方法,希望对楼主有所帮助。

在父窗口中获取iframe中的元素
1、js代码

(1)第一种方法
格式:window.frames["iframe的name值"].document.getElementById("iframe中控件的ID").click();
//window.frames["mainIframe"].document.getElementById("iTextValue").value;
//触发iframe中的元素的单击事件
//window.frames["mainIframe"].document.getElementById("iTestClick").click();

(2)第二种方法
格式:
var obj=document.getElementById("iframe的ID").contentWindow;
var ifmObj=obj.document.getElementById("iframe中控件的ID");
ifmObj.click();
//var obj=document.getElementById("mainIframe").contentWindow;
//var ifmObj=obj.document.getElementById("iTestClick");
//ifmObj.click();

2、jquery代码

(1)第一种方法


格式:$("#iframe的ID").contents().find("#iframe中的控件ID").click();
//$("#mainIframe").contents().find("#iTextValue").val();

(2)第二种方法
格式:$("#iframe中的控件ID", window.frames("frame的name").document).click();
//$("#iTextValue",window.frames["mainIframe"].document).val();

在iframe中获取父窗口的元素
1、js代码

格式:window.parent.document.getElementById("父窗口的元素ID").click();
//window.parent.document.getElementById("testClick").click();

2、jquery代码

格式:$('#父窗口中的元素ID', parent.document).click();
//$('#textValue', parent.document).val();

[解决办法]

JScript code
window.parent.document.getElementById("searchForm表单中某元素的ID");
[解决办法]
window.parent 父窗口
top 顶层窗口
[解决办法]
结贴给分。。。。

读书人网 >JavaScript

热点推荐