Javascript iframe内的输入框未触发FocusOut事件
我在iframe中有一个输入框。我在输入框上附加了Javascript iframe内的输入框未触发FocusOut事件,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我在iframe中有一个输入框。我在输入框上附加了keypress、input和focusout事件监听器,如下所示: $("iframe").contents().find('#testDiv').on('keypress input focusout', 'input', function(event) { alert(event.type); }); iframe内的输入框上会触发按键和输入事件。但是,focusout事件不会在输入框模糊时触发 如果输入框未放置在iframe中,则相
keypress
、input
和focusout
事件监听器,如下所示:
$("iframe").contents().find('#testDiv').on('keypress input focusout', 'input', function(event) {
alert(event.type);
});
iframe内的输入框上会触发按键
和输入
事件。但是,focusout
事件不会在输入框模糊时触发
如果输入框未放置在iframe中,则相同的语法和事件绑定可以正常工作。所有这三个事件都会在不在iframe内的输入框上触发
注意:我使用事件委派,因为iframe中的内容是动态的
HTML标记:
<iframe>
<body>
</body>
</iframe>
<div id="testDiv1">
<input type="text" />
</div>
var $iframe = $("iframe");
var iframe = $iframe[0];
var doc = iframe.document;
var content = '<div id="testDiv"><input type="text" /></div>';
if (iframe.contentDocument) {
doc = iframe.contentDocument;
} else if (iframe.contentWindow) {
doc = iframe.contentWindow.document;
}
doc.open();
doc.writeln(content);
doc.close();
$("iframe").contents().find('#testDiv').on('keypress input focusout', 'input', function(event) {
alert(event.type);
});
$('#testDiv1').on('keypress input focusout', 'input', function(event) {
alert(event.type);
});
Javascript代码:
<iframe>
<body>
</body>
</iframe>
<div id="testDiv1">
<input type="text" />
</div>
var $iframe = $("iframe");
var iframe = $iframe[0];
var doc = iframe.document;
var content = '<div id="testDiv"><input type="text" /></div>';
if (iframe.contentDocument) {
doc = iframe.contentDocument;
} else if (iframe.contentWindow) {
doc = iframe.contentWindow.document;
}
doc.open();
doc.writeln(content);
doc.close();
$("iframe").contents().find('#testDiv').on('keypress input focusout', 'input', function(event) {
alert(event.type);
});
$('#testDiv1').on('keypress input focusout', 'input', function(event) {
alert(event.type);
});
var$iframe=$(“iframe”);
变量iframe=$iframe[0];
var doc=iframe.document;
var内容=“”;
if(iframe.contentDocument){
doc=iframe.contentDocument;
}else if(iframe.contentWindow){
doc=iframe.contentWindow.document;
}
doc.open();
书面文件(内容);
doc.close();
$(“iframe”).contents().find(“#testDiv”).on('keypress input focusout','input',函数(事件){
警报(事件类型);
});
$('#testDiv1')。on('keypress input focusout','input',function(事件){
警报(事件类型);
});
以下是我尝试过的模型:
请帮忙!!我无法从iframe中移出输入框。首先,focusin和focusout是IE制作的两个支持冒泡的事件。但是,jquery会为其余浏览器模拟此事件 其次,只有受信任的目标才能用于事件委派(请参见链接:) 即使将事件定位到输入(见下文),也只会触发模糊和聚焦(在Chrome中测试)
所以我想,没有直接的答案。您可能需要在iframe内容上添加一些JS,以便将此事件传送到父帧。首先,focusin和focusout是IE制作的两个支持冒泡的事件。但是,jquery会为其余浏览器模拟此事件 其次,只有受信任的目标才能用于事件委派(请参见链接:) 即使将事件定位到输入(见下文),也只会触发模糊和聚焦(在Chrome中测试)
所以我想,没有直接的答案。您可能需要在iframe内容上添加一些JS,以便将此事件传递到父帧。这正是我刚才测试的内容。我可以确认焦点和模糊是iframeDang中唯一触发的事件!这是否意味着我无法在iframe中委派
blur
或focus
事件?非常感谢您提供的信息:)。我应该接受这个答案,但会等待更多的回应。这正是我刚才测试的。我可以确认焦点和模糊是iframeDang中唯一触发的事件!这是否意味着我无法在iframe中委派blur
或focus
事件?非常感谢您提供的信息:)。我应该接受这个答案,但会等待更多的回应。