怎么用JS改变iframe标签的ID值

JavaScript010

怎么用JS改变iframe标签的ID值,第1张

首先要得到iframe元素的对象可以根据元素的id,name,class等,

<iframe id="xx1" class="xx2" name="xx3"/>

document.getElementById("xx1").id="xx"//根据id得到对象 改变id值

document.getElementsByClassName("xx2")[0].id="xx"//根据class得到对象 改变id值

document.getElementsByName("xx3")[0].id="xx"//根据name得到对象 改变id值

因为class和name可以重复,所有得到的对象集合,所以后面要跟下标如:document.getElementsByClassName("xx2")[0] 中的[0] 得到第一个对象

页可以循环这个对象,将所有匹配的iframe的id值都改变,如:

var a=document.getElementsByClassName("xx2")

for (var i = 0i <a.lengthi++) {

a[i].id="xx"+i

}

注意,后面+i主要是为了id不重复。

网页加载的时候,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里面的还没加载完当然就更改不了

1.纯js的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head>

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

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

<script language="javascript">

function c(){

var a

try{

if (document.all){

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

}else{

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

}

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

b.disabled = true

clearInterval(d)

}

catch(ex){

}

}

</script>

</head>

<body>

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

</body>

</html>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head>

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

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

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

<script language="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>

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

</body>

</html>

b.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

<head>

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

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

</head>

<body>

<form name="cform"><input type="text" name="b" /></form>

</body>

</html>