this solve my problem perfectly

来源:互联网 发布:java 跳出for循环 编辑:程序博客网 时间:2024/06/05 08:18

i struggled with this problem 4days ,and i solve it from here:

http://bbs.csdn.net/topics/390878194

1.在加载完后再执行
2.是button.onclick=function(),不是button.click=function()

JavaScript code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!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>a</title>
<script type="text/javascript">
    function alert_(){
        var iframe=document.getElementById("frame").contentWindow;   
        alert(iframe.document.getElementById("div").innerHTML);
    }
      
    function update_(){
        var iframe=document.getElementById("frame").contentWindow;   
        iframe.document.getElementById("div").innerHTML="七上八下";
    }
    function doit(){
    //给b页面绑定点击事件    这里为什么不行呢。。。  求解决
    var button=document.getElementById("frame").contentWindow.document.getElementById("button2");
    button.onclick=function(){
        alert("555555555");   
    }
    }
      
</script>
</head>
  
<body onload="doit()">
<div>
<input type="button" value="弹出iframe中div的值" onclick="alert_()" id="button1"/>
<input type="button" value="修改iframe中div的值" onclick="update_()"/>
</div>
<iframe src="b.html" frameborder="1" width="600" height="100" style="border:#F00" id="frame"/>
</body>
</html>


子窗口:b.html
XML/HTML code
?
1
2
3
4
5
6
7
8
9
10
11
<!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>b</title>
</head>
<body>
<input type="button" id="button2" value="修改a中的弹出按钮的值"/>
<div id="div" style=" border:1px solid yellow;">乱七八糟</div>
</body>
</html>




JavaScript code
?
1
2
3
4
5
6
7
8
9
<iframe src="b.html" frameborder="1" width="600" height="100" style="border:#F00" id="frame"/>
<script type="text/javascript">
var button=document.getElementById("frame").contentWindow.document.getElementById("button2");
    button.onclick=function(){
        alert("555555555");   
    }
</script>
 
</body>











原创粉丝点击