iframe 文档上的按键将整个 html 作为目标(不是确切的节点)
我有一个带有 designMode="on" 的 iframe (是的 - 我知道这是件坏事)
我应该点击它并按键并回显目标元素的节点名称。
$(function() {
var editor = $("#editor")[0].contentWindow;
var doc = editor.document;
editor.document.designMode = "on";
doc.open();
doc.write('<div id="dummy">test</div>');
doc.close();
// find iframe body
var $body = $("#editor").contents().find('#dummy').parent();
// clean after finding
$body.html('<div>Hello</div>');
var report = function(e) {
$("#result").html(
$("#result").html() + " " + e.target.nodeName.toLowerCase());
};
$body.click(report);
// $body.keypress(report) -> doesn't work
// only $(doc).keypress works:
$(doc).keypress(report);
});
当我点击单词“Hello”时 - 我得到“div” - 这是正确的,但是当我按下它时 - 我得到“html”而不是“div”。如何解决这个问题?
I have an iframe with designMode="on" (Yeah - I know this is bad thing)
I should catch clicking on it and keypressing and echo the node name of target element.
$(function() {
var editor = $("#editor")[0].contentWindow;
var doc = editor.document;
editor.document.designMode = "on";
doc.open();
doc.write('<div id="dummy">test</div>');
doc.close();
// find iframe body
var $body = $("#editor").contents().find('#dummy').parent();
// clean after finding
$body.html('<div>Hello</div>');
var report = function(e) {
$("#result").html(
$("#result").html() + " " + e.target.nodeName.toLowerCase());
};
$body.click(report);
// $body.keypress(report) -> doesn't work
// only $(doc).keypress works:
$(doc).keypress(report);
});
When I click on word "Hello" - I get "div" - it's correct, but when I keypress on it - i get "html" instead "div". How to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我创建了一个函数,它可以按按键工作。
但 $(doc).keypress(reportDomPath) 仅适用于 FF。
I created a function, it can work keypress.
But $(doc).keypress(reportDomPath) only works on FF.
我做了一些谷歌..这可能对你有帮助:
所有关于jetcook的信条(http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_25589672.html)
I did a bit of google .. this might help you:
all credis to jetcook (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_25589672.html)
您最好避免使用 Javascript 添加这些事件,我一生都无法使用 FF5 的 javascript 触发任何事件。我发现你的 Editor.html 页面应该看起来像这样
这有最多的浏览器支持,也是最简单的方法。
You are best to avoid adding these events with Javascript, I could not get any event to fire using javascript with FF5 for the life of me. What I found was that your Editor.html page should look something like this
This has the most browser support and is the simplest way of doing it too.