js如何修改iframe 中元素的属性
2.htm中的内容 “<form><input name=b ></form>”
现在js代码在1.htm中,如何通过js把2.htm中,input 的disabled 改成true
我写的代码如下,使用起来并不对,请大家指正
<script>
var a=document.getElementById("a");
b=a.document.getElementsByName("b");
b.disabled=true;
</script>
1楼的,你的方法我试了 不对吧
1楼的,你试着把触发的<a>去掉,让网页加载就自动把input的属性更改,你会发现,你的写法是用不了的,我也测试了,当然,有a触发是能用的 展开
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>
网页加载的时候,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>