我如何观察“a”的内容标签-jquery
I have a blank <a>
tag that content is loaded into via an external piece of javascript. I want to observe the <a>
and when its content changes perform another task. The content will only ever change once.
Can this be done?
I am using also using jQuery.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 jQuery && 的混合物。 DOM Level 3 事件(请参阅下面的浏览器支持)。
如果您想检查内容中的任何更改,可以执行以下操作:
查看此代码的实际操作:http://jsfiddle.net/wJbMj/1/
参考:DOMNodeInserted, DOMAttrModified
虽然上述解决方案实际上对我来说非常方便,但它仅适用于支持这些事件的浏览器。要获得更通用的解决方案,您可以挂钩 jQuerys setter 方法。该解决方案的缺点是,您只能捕获通过 jQuery 完成的更改。
您可以以完全相同的方式挂钩
.text()
和.html()
。您需要检查覆盖方法中的this
值是否代表正确的 DOMnode。You can use a mixture out of jQuery && DOM Level 3 events (see browser support below).
If you want to check for any changes within the content, you could do this:
See this code in action: http://jsfiddle.net/wJbMj/1/
Reference: DOMNodeInserted, DOMAttrModified
While the above solution is actually pretty convinient to me, it'll only work in browser that support those events. To have a more generic solution, you can hook into jQuerys setter methods. The downside in this solution is, that you will only catch changes that were done through jQuery.
You could hook into
.text()
and.html()
the exact same way. You would need to check if thethis
value within the overwritten methods represent the correct DOMnode.您可以尝试监视标签的 .html() 以查看它是否更改为其他内容...
也许有一个定时函数(每 n 秒执行一次)来监视元素的内容 (.html()) 直到它更改,然后停止监视它。也许是这样的:
理论上这应该有效,但我还没有测试过。
You can try monitoring the .html() of the tag to see if it changes to anything else...
Maybe have a timed function (executing every n-seconds) that monitors the content (.html()) of the element until it changes and then stops monitoring it. Maybe something in the vein of:
In theory this should work, but I haven't tested it.
您可以利用 DOMSubtreeModified 事件 。当元素的内容发生更改时,该事件会在元素上触发。
注意:此事件不会在 Opera 和旧版本的 IE 中触发(但在 IE9 中有效)。
现场演示: http://jsfiddle.net/simevidas/pLvgM/
You could make use of the DOMSubtreeModified event. That event fires at an element when it's contents change.
Note: this event does not fire in Opera and older versions of IE (it works in IE9 though).
Live demo: http://jsfiddle.net/simevidas/pLvgM/
a 标签的内容是什么意思?您可以像这样执行此操作:
或者获取像这样的属性:
我不认为标签更改时会触发事件,
.change()
事件仅由输入区域触发并选择列表。您需要在加载新插入内容的事件之后进行检查。
What do you mean content of an a tag? You can do this like so:
or get an attribute like so:
I don't think there is an event that is fired when a tag changes, the
.change()
event is only fired by input areas and select lists.You would need to check after the event that loads the newly inserted content.