目的:获取iframe中input的值123
打开 index.html
用js无法操作iframe,求助!
两个网页如下:
index.html<html>
<head>
<title>我的百度</title>
</head>
<body>
<div>
<input type="button" value="百度" onclick="myFun()">
<input type="button" id="kk" value="abc" onclick="abc()">
</div>
<div name="div2" src="abc.html">
<iframe src="abc.html" name="fr2" id="ppp"></iframe>
</div>
<script>
function myFun(){
//var obj1 = parent.document.getElementById("ppp")
var obj1 = document.frames[0].window.getElementById("kw")
alert(typeof obj1)
}
function abc(){
var obj2 = document.getElementById("kk")
obj2.type="text"
}
</script>
</body>
</html>
iframe中的网页如下
abc.html<html>
<head>
<title>abc</title>
</head>
<body>
<div>
<input type="text" id="kw" value="123">
<input type="button" id="a1" value="test" onclick="bu1()">
</div>
<script>
function bu1(){
alert("999")
}
</script>
</body>
</html>
哎!操作这个很少有人用的,因为可以调用别人网页的资源,所以是只读的,我给你重写你的index.html的代码吧 <html>
<head>
<title>我的百度</title>
<meta charset="utf-8">
</head>
<body>
<div>
<input type="button" value="百度" onclick="myFun()">
<input type="button" id="kk" value="abc" onclick="abc()">
</div>
<div name="div2" src="abc.html">
<iframe src="abc.html" name="fr2" id="ppp"></iframe>
</div>
<script>
function myFun(){
var obj1 = document.getElementById("ppp")
obj1=obj1.contentWindow.document.getElementById('kw')
alert(obj1.value)
}
function abc(){
var obj2 = document.getElementById("kk")
obj2.type="text"
}
</script>
</body>
</html>
|