简体   繁体   English

当div中的内部html改变时的火灾事件

[英]Fire event when inner html in div changed

I have div for some informations, which are filled into with .innerHTML by clicking on the button. 我有一些信息的div ,通过单击按钮填充.innerHTML The goal is I want to .slideDown the div when the text in div is added. 目标是我想在.slideDown div中的文本时.slideDown div。 Is it possible to do it with jQuery ? 有可能用jQuery做到吗?

Example: 例:

<div id="calcInfo"></div>

Adding text into div - 将文本添加到div中 -

document.getElementById("calcInfo").innerHTML = 'someText';

I want to use this function after it :) 我想在它之后使用这个功能:)

if ($('#calcInfo').text().trim().length > 0) {
   $('#calcInfo').slideDown('slow', function() {});
};

I was trying .change() function, but it only works for input and textarea elements. 我正在尝试.change()函数,但它只适用于inputtextarea元素。 Many thanks for answer ! 非常感谢您的回答!

Ondrej 的Ondrej

You can do it by extending the $.html function. 你可以通过扩展$ .html函数来实现。 Like this: 像这样:

(function($)
{
    var oldHtml = $.fn.html;
    $.fn.html = function()
    {
        var ret = oldHtml.apply(this, arguments);

        //trigger your event.
        this.trigger("change");

        return ret;
    };
})(jQuery);

Attach event handler: 附加事件处理程序

$("#calcInfo").on("change",function(){
     if ($(this).text().trim().length > 0) {
        $(this).slideDown('slow', function() {});
     };
});

When you need to change your html. 当你需要更改你的HTML。 Do it like this: 像这样做:

$("#calcInfo").html('someText');

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

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