js如何修改iframe 中元素的属性?

JavaScript015

js如何修改iframe 中元素的属性?,第1张

网页加载的时候,iframe里面的还没加载完当然就更改不了。

1.纯js的

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/htmlcharset=utf-8"/>

<title>无标题文档</title>

<scriptlanguage="javascript">

functionc(){

vara

try{

if(document.all){

a=document.frames["a"].document

}else{

a=document.getElementById("a").contentDocument

}

varb=a.getElementsByName("b")[0]

b.disabled=true

clearInterval(d)

}

catch(ex){

}

}

</script>

</head>

<body>

<iframeid="a"src="b.html"onload="c()"></iframe>

</body>

</html>

2.使用jQuery框架的,建议使用,封装了js,操作DOM等等非常方便

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/htmlcharset=utf-8"/>

<title>无标题文档</title>

<scriptlanguage="javascript"src="/js/jquery-1.4.4.js"type="text/javascript"></script>

<scriptlanguage="javascript"type="text/javascript">

<!--//

$(document).ready(function(){

if($.browser.msie){

$('#a').ready(function(){

$(window.frames["a"].document).find("input").attr("disabled","disabled")

})

}

else{

$('#a').load(function(){

$('#a').contents().find("input").attr("disabled","disabled")

})

}

})

//-->

</script>

</head>

<body>

<iframeid="a"name="a"src="iframechild.html"></iframe>

</body>

</html>

b.html

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/htmlcharset=utf-8"/>

<title>无标题文档</title>

</head>

<body>

<formname="cform"><inputtype="text"name="b"/></form>

</body>

</html>

首先你要能获取到这个iframe的dom对象,假设我们知道它的ID;

var frame = document.getElementById("ueditor_0")

var frameDocument = frame.contentWindow.document

iframe中的document对象拿到了,后边如何获取你想要的元素就很简单了吧。

原理就是先在当前页面中找到你的Iframe对象

var

oframe

=

document.getElementById("iframe")

再将iframe对象看着是另一个页面的文本对象

var

childDome

=

oframe.document.getElementById("H")

简洁点的就是:document.getElementById("iframe").document.getElementById("H")

或者是:document.getElementById("Result").contentWindow.document.getElementById("h")