日期:2014-05-16  浏览次数:20347 次

请教一个iframe相互调用dom的问题
[code=text]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>子页面禁止选中/复制,不兼容firefox</title>
</head>
<body>
    <script type="text/javascript" >
        function test() {
            window.frames["aaa"].document.oncontextmenu = new Function("return false;");
            window.frames["aaa"].document.onselectstart = new Function("event.returnValue=false"); 
        }
    </script>
    <iframe id="aaa" name="aaa" src="b.htm" height="800" width="800"></iframe>
    <input type=button onclick="test()" value="aaa" />
</body>
    <script type="text/javascript">
           
    </script>
</html>
[/code]
代码如上
点击按钮则禁用掉iframe中的右键菜单和选中.
问题是兼容firefox,以及当我在test中增加一个更改iframe的src属性就会使得禁用失效的问题
[code=text]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>子页面禁止选中/复制,不兼容firefox</title>
</head>
<body>
    <script type="text/javascript" >
        function test() {
            document.getElementById("aaa").src = "b.htm";
            window.frames["aaa"].document.oncontextmenu = new Function("return false;");
            window.frames["aaa"].document.onselectstart = new Function("event.returnValue=false"); 
        }
    </script>
    <iframe id="aaa" name="aaa" src="b.htm" height="800" width="800"></iframe>
    <input type=button onclick="test()" value="aaa" />
</body>
    <script type="text/javascript">
           
    </script>
</html>
[/code]
------解决方案--------------------
document.getElementById("aaa").src = "b.htm";
//页面重新加载了,将你的事件处理函数定义在onload事件函数内.
document.getElementById("aaa").onload=function(){
    window.frames["aaa"].document.oncontextmenu=...){};
}
------解决方案--------------------
firefox不支持onselectstart事件,禁止选择用css控制,而且要在iframe onload中设置

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>子页面禁止选中/复制,不兼容firefox</title>
</head>
<body>
    <script type="text/javascript" >
function addEvent(){
var doc=document.getElementById("aaa").contentWindow.document;
doc.oncontextmenu = function(){parent.document.title=new Date().getTime();return false}
doc.onselectstart = function(){