網(wǎng)上看了別人的獲取值的例子,我又加上了修改iframe里元素值的部分供大家參考
主頁面:1.html
<html>
<head>
<title>jquery方式,訪問iframe頁面dom元素</title>
<meta name="Author" content="孫勤波">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/static/jquery/jquery-1.10.2.min_f2fb5194.js"></script>
</head>
<body>
<iframe src="2.html" id="frame_1"></iframe>
<br />
<a id="set_target" href="javascript:getIframeDom();">獲取iframe 頁面test元素內(nèi)容</a>
<a id="stest" href="aaaa">bbb</a>
<script type="text/javascript">
function getIframeDom(){
// 獲取iframe的test元素
var test = $("#frame_1").contents().find("#test");
alert(test.html());
var lurl= $("#frame_1").contents().find("#url_location");
alert(lurl.html());
lurl.attr("target","_blank");
}
var $iFrame=$("#frame_1");
$iFrame.prop("src","http://ny.zt.gov.cn/temp/2.html");
$iFrame.load(function(){
//alert("加載完畢");
var test = $("#frame_1").contents().find("#test");
alert(test.html());
var lurl= $("#frame_1").contents().find("#url_location");
alert(lurl.html());
lurl.attr("target","_blank");
});
</script>
</body>
</html>
Iframe頁面:2.html
<html>
<head>
<title>iframe頁面a.html</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<div id="test">我是測試頁面test</div>
<a id="url_location" href="3.html">open the new window</a>
</body>
</html>
聯(lián)系客服