如何检测 HTML 元素内容的变化?
我正在使用 Ruby on Rails
3.1.0
和 jquery-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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
工作示例: JSFiddle
此方法比其他两种方法更好,因为它不使用轮询。相反,它使用观察者模式,这要好得多。然而,缺点是这在低于 9 的 Internet Explorer 版本中不起作用。
How about:
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.
这个怎么样:
用法:
How about this:
Usage:
这可能是一个“duh”的答案,但老实说,我认为最好的选择是简单地修改“更改 DIV”函数,以便它调用另一个函数:
这不像使用 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:
This isn't as cool as using crazy events like DOMSubtreeModified ... but it will work in all browsers.