简体   繁体   English

html内容的onchange事件

[英]onchange event of html content

I have a textarea , and using a JQuery plugin I convert it to a simple HTML editor. 我有一个textarea ,并使用JQuery插件将其转换为简单的HTML编辑器。

The problem is that I don't know how to asign the onchange event to the html editor, since asigning the event to the textarea itself won't work. 问题是我不知道如何将onchange事件分配给html编辑器,因为将事件分配给textarea本身是行不通的。

The plugin creates an iframe and the HTML inside this iframe is the html displayed on the html editor. 该插件会创建一个iframe,并且该iframe中的HTML是html编辑器上显示的html。

I also manage to get the HTML content using: 我还设法使用以下方法获取HTML内容:

var html = $("#txt_extradata").closest("tr").find(".sceditor-container iframe").contents().find("html").html();

Now, how can I write an event to detect if this html changes through time? 现在,我该如何编写一个事件来检测此html是否随时间变化?

You need to bind to the body of the iframe by using a contextual selector like so: 您需要通过使用上下文选择器来绑定到iframe的主体,如下所示:

$(function() { 
     var ctx = $(".sceditor-container iframe").contents();
     $('body', ctx).blur(function() {
         //detect changes to the widget
     });
});

This will bind a keydown event to the body tag of the iframe. 这会将keydown事件绑定到iframe的body标签。 You may decide that keydown is not the appropriate event for your needs but regardless, this will get you to where you can simply bind the event you want. 您可能会认为keydown并不是适合您需要的事件,但是无论如何,这将使您到达可以简单地绑定所需事件的位置。

EDIT: 编辑:

Here is a jsfiddle for proof of concept http://jsfiddle.net/SJQZN/ 这是用于概念验证的jsfiddle http://jsfiddle.net/SJQZN/

Unfortunately, I couldn't quite get jsfiddle to properly load the css from their github repository so I made do with a quick hack up of my own. 不幸的是,我不能让jsfiddle从他们的github仓库正确加载css,所以我做了一个自己的快速技巧。

Combining answers from here and here . 结合这里这里的答案。 You now have this 你现在有这个

$(".sceditor-container iframe").contents().find("body")
    .on("input propertychange", function() {
        // your method.
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM