English 中文(简体)
如何发现超文本元素内容的变化?
原标题:How to detect a change in the contents of a HTML element?

我使用的是Ruby on Railways3.1.0jquery-rails gem。 我愿将一次j Query活动(也许我可以使用liveFunction...)约束在“超文本div标签上,以便我能够检查其内容的变化,如果是的话(即如果在“div tag”上添加新的代码的话),则在另一个“超文本”上形成一个“硬码>div。

在我看来,这就是:

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

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

I would like to add emove 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>

我愿努力在<条码>div和id=“div_content_2”中取得以下产出:

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

www.un.org/Depts/DGACM/index_spanish.htm 我怎样做?

P.S.: 我正在执行一种表格,用户在选择某种选择后(该活动旨在产生超文本input。 <代码>div with id=“div_content_1> - 我想为了我的习惯目的,可以在同一页(div with id="div_content_2>)上订购这些选择,是为了使I计划进行的分类过程成为可能。

问题回答

如何做到这一点:

($.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!");
});

这可能是一种“duh”的答案,但我真诚地认为,你的最好之点是简单地修改你的“DIV”功能,使之具有另一个功能:

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

它是使用诸如“DOMSubtreeModised”等高频事件的冷却,但它将在所有浏览器中工作。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签