如何检测 HTML 元素内容的变化?

发布于 2024-12-09 03:08:25 字数 1321 浏览 1 评论 0原文

我正在使用 Ruby on Rails 3.1.0jquery-rails gem。我想将 jQuery 事件(也许我可以使用 live 功能...)绑定到 HTML div 标记,以便我可以检查其内容更改,如果so(即,如果将新代码添加到该 div 标记中),在另一个 HTML div 标记中创建自定义文本。

也就是说,在我的视图文件中,我有:

<div id="div_content_1"></div>

<div id="div_content_2"></div>

我想添加\删除“Hello!” div 内带有 id="div_content_2" 的文本消息,每次 div content 带有 id=" div_content_1" 发生变化(在我的例子中,当 HTML input 字段添加到该 div 标记时 - 请阅读下面的示例)。例如(在“add”情况下),当带有 id="div_content_1"div 像这样更改时,

<div id="div_content_1">
  <!-- The following 'input' tag was just added -->
  <input ... />
</div>

我想让它工作,以便有一个输出就像 div 中的 id="div_content_2" 中的以下内容一样:

<div id="div_content_2">
  <p>
    Hello!
  </p>
</div>

我该怎么做?

PS:我我正在实现一个表单,用户在选择某个选项后(此事件旨在使用 id="div_content_1"div 中生成 HTML input 字段 - 我出于自定义目的而这样做...)可以在同一页面上订购这些选项(带有 id="div_content_2"div 旨在使我计划的排序过程成为可能)。

I am using Ruby on Rails 3.1.0 and the jquery-rails gem. I would like to bind a jQuery event (maybe I could use the live functionality...) to a HTML div tag so that I can check its content changes and, if so (that is, if new code is added to the that div tag), to create a custom text in another HTML div tag.

That is, in my view file I have:

<div id="div_content_1"></div>

<div id="div_content_2"></div>

I would like to add\remove an "Hello!" text message inside the div with id="div_content_2" eachtime the div content with id="div_content_1" changes (in my case, when an HTML input field is added to that div tag - read the example that follows). For example (in the "add" case), when the div with id="div_content_1"changes like this

<div id="div_content_1">
  <!-- The following 'input' tag was just added -->
  <input ... />
</div>

I would like to make it to work so to have an output like the following in the div with id="div_content_2":

<div id="div_content_2">
  <p>
    Hello!
  </p>
</div>

How can I do that?

P.S.: I am implementing a form for which a user, after selecting some option (this event is intended to generate HTML input fields in the div with id="div_content_1" - I make that for my custom purposes...) can order those option on the same page (the div with id="div_content_2" is intended to make possible the sorting process that I planned).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

财迷小姐 2024-12-16 03:08:25

怎么样:

$('#div_content_1').bind('DOMSubtreeModified', function() {
    $('#div_content_2').html('<p>Hello</p>');
});

工作示例: JSFiddle

此方法比其他两种方法更好,因为它不使用轮询。相反,它使用观察者模式,这要好得多。然而,缺点是这在低于 9 的 Internet Explorer 版本中不起作用。

How about:

$('#div_content_1').bind('DOMSubtreeModified', function() {
    $('#div_content_2').html('<p>Hello</p>');
});

Working example: JSFiddle

This method is better than the other two because it does not use polling. Instead, it uses the Observer pattern, which is much better. However, the downside is that this won't work in versions of Internet Explorer lower than 9.

少跟Wǒ拽 2024-12-16 03:08:25

这个怎么样:

($.fn.watchChanges = function(cb){
    $(this).each(function(i, e){
        $(e).data("original_content", $(e).html());
        window.content_changes = window.content_changes ? window.content_changes : [];
        window.content_changes.push({"element": e, "callback": cb});
    });
    return $(this);
})(jQuery);

setInterval(function(){
    if(window.content_changes)
    {
        $.each(window.content_changes, function(i, e) {
            if($(e.element).data("original_content") != $(e.element).html())
            {
                e.callback.apply($(e.element));
                $(e.element).data("original_content", $(e.element).html())
            }
        });
    }
}, 500);

用法:

$('.foo').watchChanges(function(){
    $('.bar').html("something's different about you, foo!");
});

How about this:

($.fn.watchChanges = function(cb){
    $(this).each(function(i, e){
        $(e).data("original_content", $(e).html());
        window.content_changes = window.content_changes ? window.content_changes : [];
        window.content_changes.push({"element": e, "callback": cb});
    });
    return $(this);
})(jQuery);

setInterval(function(){
    if(window.content_changes)
    {
        $.each(window.content_changes, function(i, e) {
            if($(e.element).data("original_content") != $(e.element).html())
            {
                e.callback.apply($(e.element));
                $(e.element).data("original_content", $(e.element).html())
            }
        });
    }
}, 500);

Usage:

$('.foo').watchChanges(function(){
    $('.bar').html("something's different about you, foo!");
});
一袭水袖舞倾城 2024-12-16 03:08:25

这可能是一个“duh”的答案,但老实说,我认为最好的选择是简单地修改“更改 DIV”函数,以便它调用另一个函数:

function addInputToDiv(newInput) {
    div1.append(newInput);
    takeSomeActionToDiv2InResponse();
}

这不像使用 DOMSubtreeModified 这样的疯狂事件那么酷。 .但它适用于所有浏览器。

This may be a "duh" kind of answer, but honestly I think your best bet is to simply modify your "change the DIV" function so that it calls another function:

function addInputToDiv(newInput) {
    div1.append(newInput);
    takeSomeActionToDiv2InResponse();
}

This isn't as cool as using crazy events like DOMSubtreeModified ... but it will work in all browsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文