jquery 监听 iframe 中的事件
我正在使用 jquery 制作一个非常简单的富文本编辑器...我不想使用第三方的。
我需要从输入开始监听 iframe(同一域等)内的事件。显然我需要经常使用bind()。
这就是我目前所拥有的,它在 IE8 中运行良好(足够令人惊讶),但在 Chrome 中却不行。
<script>
$(function() {
$('#text').contents().bind("keyup keydown keypress", function(e) {
var code = e.keyCode || e.which;
alert(code);
return false;
});
});
</script>
<body>
<iframe id="text" name="text" src="edit.html"></iframe>
</body>
在上面的按键事件中,我还想获取“edit.html”的当前值并用该值更新文本区域...
任何帮助将不胜感激:)
非常感谢
编辑:进一步解释,编辑。 html 是一个可编辑的文件,使用 "document.body.contentEditable = true;"
-
编辑 2:
edit.html =
<script language="javascript">
function InitializeIFrame() {
document.body.contentEditable = true;
}
</script>
<html>
<body onload="InitializeIFrame();">
</body>
</html>
I'm making a very simple Rich Text Editor using jquery... I don't want to use a third-party one.
I need to listen for events within an iframe (same domain etc), starting with typing. Apparently I'll need to use bind() a lot.
This is what I've got at the moment which works fine in IE8 (amazingly enough) but not Chrome.
<script>
$(function() {
$('#text').contents().bind("keyup keydown keypress", function(e) {
var code = e.keyCode || e.which;
alert(code);
return false;
});
});
</script>
<body>
<iframe id="text" name="text" src="edit.html"></iframe>
</body>
On the key press event above, I will also want to get the current value of 'edit.html' and update a textarea with that value...
Any help would be MUCH appreciated :)
Many thanks
EDIT: to explain further, edit.html is an editable file using "document.body.contentEditable = true;"
-
EDIT 2:
edit.html =
<script language="javascript">
function InitializeIFrame() {
document.body.contentEditable = true;
}
</script>
<html>
<body onload="InitializeIFrame();">
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我似乎记得当我尝试与 iframe(同一域等)通信时遇到了问题。我发现的技巧是在框架内进行绑定并绑定到主窗口中的函数。像这样的东西(在edit.html中):
然后在主页中像这样的东西:
我意识到这并不完全符合您尝试执行的方式,但达到了相同的效果。根据我对 javascript 和 iframe 的理解,实际上,与家长沟通比与 iframe 沟通更容易。如果您确实需要双向通信,您可以(继续上面的示例)使用函数 funcKey() 的返回值将数据传递回 iframe。
I seem to recall running into a problem when I was trying to communicate with an iframe (same domain etc). The trick I found is to do the binding inside the frame and bind to functions in the main window. Something like this (in edit.html):
and then in the main page something like:
I realise this does not exactly fit into the way you were trying to do it, but the same effect is achieved. From what I understand of javascript and iframes, in practice it's easier to communicate with a parent than it is to communicate with an iframe. If you really need two-way communication you could (going on the example above) use the return value of the function funcKey() to pass data back into the iframe.
关于 javascript 最烦人的事情之一是 document.frames["frameID"] 返回与 document.getElementById("frameID") 不同的对象。 (这就是您使用 $("#frameID") 时得到的结果)
这种区别可能就是您遇到跨浏览器问题的原因。您可能需要尝试不同的方法来访问 iframe 内容,直到找到一种可以正常工作的方法。
One of the most annoying things about javascript is that document.frames["frameID"] returns a different object than document.getElementById("frameID"). (which is what you get when you use $("#frameID"))
This distinction is probably why you're running into cross-browser issues. You'll likely need to mess around with different ways of accessing the iframe contents until you find one that works correctly.