Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按enter键时parent.alert事件未着火_Javascript_Html_Javascript Events - Fatal编程技术网

Javascript 按enter键时parent.alert事件未着火

Javascript 按enter键时parent.alert事件未着火,javascript,html,javascript-events,Javascript,Html,Javascript Events,我有一个主jsp页面,其中使用 window.alert=function(txt) 在其他jsp中,使用iframe将页面加载到同一文档中。这些页面中的大多数都使用parent.alert来保持一致性 这是我正在使用的一段代码 <div onkeypress="handlekey()"> <button id='done' onclick="example()" >submit</button> </div> 提交 脚本部分如下所

我有一个主jsp页面,其中使用

window.alert=function(txt)
在其他jsp中,使用iframe将页面加载到同一文档中。这些页面中的大多数都使用parent.alert来保持一致性

这是我正在使用的一段代码

<div onkeypress="handlekey()">
    <button id='done' onclick="example()" >submit</button>
</div>

提交
脚本部分如下所示

<script>
    function handlekey(){
        if ((window.event) && (window.event.keyCode == 13))
        {
            // click the search button
            done.click();
        }
    }
    function example(){
        parent.alert('Done');
    }
</script>

函数handlekey(){
if((window.event)&&(window.event.keyCode==13))
{
//单击搜索按钮
完成。单击();
}
}
函数示例(){
parent.alert(“完成”);
}
如果我按enter键,则不会触发警报,但当我单击按钮时会触发警报。
当我将parent.alert替换为alert时,也会触发alert。

这对我来说很好:

index.html

<html>
<head>
    <script>
    window.alert = function(){
        console.log('My alert');
    }
    </script>
</head>
<body>
    <iframe src="iframe.html" id="myIframe"/>
</body>
</html>
<html>
<head>
    <script>
    function handleEvent() {
        if ((window.event) && ((window.event.keyCode == 13) || window.event.type == 'click')) {
            example();
        }
    }

    function example(){
        parent.alert();
    }
    </script>
</head>
<body>
    <div onkeypress="handleEvent()">
        <button id='done' onclick="handleEvent()" >submit</button>
    </div>
</body>
</html>

window.alert=函数(){
console.log(“我的警报”);
}
iframe.html

<html>
<head>
    <script>
    window.alert = function(){
        console.log('My alert');
    }
    </script>
</head>
<body>
    <iframe src="iframe.html" id="myIframe"/>
</body>
</html>
<html>
<head>
    <script>
    function handleEvent() {
        if ((window.event) && ((window.event.keyCode == 13) || window.event.type == 'click')) {
            example();
        }
    }

    function example(){
        parent.alert();
    }
    </script>
</head>
<body>
    <div onkeypress="handleEvent()">
        <button id='done' onclick="handleEvent()" >submit</button>
    </div>
</body>
</html>

函数handleEvent(){
如果((window.event)&&((window.event.keyCode==13)| | window.event.type==click')){
示例();
}
}
函数示例(){
parent.alert();
}
提交

它没有任何作用。如果我在函数示例()中添加另一个警报(不是父警报),它也会被触发。