js如何修改iframe 中元素的属性?
网页加载的时候,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/html; charset=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/html; charset=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/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<form name="cform"><input type="text" name="b" /></form>
</body>
</html>