p.html代码
<form action="http://www.baidu.com" method="post" name="Form0">
<input type="button" id="but2" value="button" /><br />
<iframe src="b.html"></iframe>
</form>b.html代码
<script>
function setB(){
parent.document.getElementById("but2").onclick = new Function("Form0.submit();")
}
</script>
<body onload="setB()">
<form action="http://g.cn" method="post" name="Form0">
</form>
</body>情况:p.html中的iframe装载后改变了but2按钮的onclick事件
问题:点击but2时为什么是b.html提交了,而不是p.html提交呢?这样的机制是什么?怎么解决呢?请知道的朋友赐教赐教,谢谢!!!!

解决方案 »

  1.   

    parent.document在iframe 中 调用这句话,就会将控制转回到 它所在的页面
      

  2.   


    parent.document.getElementById("but2").onclick = new Function("parent.document.Form0.submit()");
      

  3.   

    parent.document.getElementById("but2").onclick = new Function("Form0.submit();") 此时的 Form0.submit();仍然是b.html的form啊,当然是b.html提交了,改成:
    parent.document.getElementById("but2").onclick = new Function("parent.document.forms[0]  .submit();") 
      

  4.   

    那如果b.html改为parent.document.forms[0].submit();然后在p.html还要再做其他的动作,再去点but2,这样会不会出问题呢?
      

  5.   

    能不能说一说为什么要加parent呢?按理说改变它的事件,它执行的时候就是它所在页面的东西。
      

  6.   

    它所在的页面就是 iframe 引用的页面