Add event to iframe body

淺唱寂寞╮ 提交于 2019-12-25 03:14:39

问题


I am setting up a own made wysiwyg plugin for jQuery. He is almost finished but know i want to add a keyup event to a iframe with designmode on. But i can't get it worked. I tryied almost everything a found on the internet. The class of the wysiwyg iframe with designmode is .dhwysiwyg. I though the next code will work:

$(function () {
$('.dhwysiwyg', container).keyup(function () {
// the script
});
});

But this won't work. The keyup event don't exists.

Can someone help me?

Tom


回答1:


Does the page in your iframe load its own copy of jQuery? If so, try this in your outer page (the page with the iframe in it, not the page in the iframe):

$(function() {
  var frameWindow = $('.dhwysiwyg').get(0).contentWindow;
  frameWindow.$('body').keyup(function() {
    // handler
  });
});



回答2:


I'm not sure about the particulars of your example, but I know that pushing events to an iFrame will sometimes violate the security policy of your browser.




回答3:


I've never heard of this "design mode" thing (only contentEditable) but your problem might be a security limitation. What are the iframe contents, and are they on the same domain?




回答4:


this method work for me

$(function() {
      var frameWindow = $('.dhwysiwyg').get(0).contentWindow;
      $(frameWindow.document).delegate("body",'keyup',function() {
      var bodycontent=$(frameWindow.document.body).text();
    });
});


来源:https://stackoverflow.com/questions/2387589/add-event-to-iframe-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!