我如何观察“a”的内容标签-jquery

发布于 2024-10-19 21:01:25 字数 193 浏览 12 评论 0原文

我有一个空白 标签,内容通过外部 JavaScript 片段加载到其中。我想观察 并在其内容更改时执行另一项任务。内容只会改变一次。

这可以做到吗?

我也在使用 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 技术交流群。

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

发布评论

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

评论(4

西瓜 2024-10-26 21:01:25

您可以使用 jQuery && 的混合物。 DOM Level 3 事件(请参阅下面的浏览器支持)。

如果您想检查内容中的任何更改,可以执行以下操作:

var $a = $('a');

$a.one('DOMNodeInserted', function(e) {
    console.log('content changed!: ', e);    

    console.log('new content: ', $(this).html());   
});

$a.one('DOMAttrModified', function(e) {
    console.log('attribute changed!: ');        

    console.log('attribute that was changed: ', e.attrName);
});

查看此代码的实际操作:http://jsfiddle.net/wJbMj/1/

参考DOMNodeInserted, DOMAttrModified


虽然上述解决方案实际上对我来说非常方便,但它仅适用于支持这些事件的浏览器。要获得更通用的解决方案,您可以挂钩 jQuerys setter 方法。该解决方案的缺点是,您只能捕获通过 jQuery 完成的更改。

var _oldAttr = $.fn.attr;
$.fn.attr = function() {
    console.log('changed attr: ', arguments[0]);
    console.log('new value: ', arguments[1]);
    return _oldAttr.apply(this, arguments);
};

您可以以完全相同的方式挂钩 .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:

var $a = $('a');

$a.one('DOMNodeInserted', function(e) {
    console.log('content changed!: ', e);    

    console.log('new content: ', $(this).html());   
});

$a.one('DOMAttrModified', function(e) {
    console.log('attribute changed!: ');        

    console.log('attribute that was changed: ', e.attrName);
});

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.

var _oldAttr = $.fn.attr;
$.fn.attr = function() {
    console.log('changed attr: ', arguments[0]);
    console.log('new value: ', arguments[1]);
    return _oldAttr.apply(this, arguments);
};

You could hook into .text() and .html() the exact same way. You would need to check if the this value within the overwritten methods represent the correct DOMnode.

浪菊怪哟 2024-10-26 21:01:25

您可以尝试监视标签的 .html() 以查看它是否更改为其他内容...

也许有一个定时函数(每 n 秒执行一次)来监视元素的内容 (.html()) 直到它更改,然后停止监视它。也许是这样的:

var monitor = true;
var doMonitor = function() {
  monitor = $("#theanchor").html() != "the initial value";
  if (monitor)
    setTimeout(doMonitor,500);
};
setTimeout(doMonitor,500);

理论上这应该有效,但我还没有测试过。

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:

var monitor = true;
var doMonitor = function() {
  monitor = $("#theanchor").html() != "the initial value";
  if (monitor)
    setTimeout(doMonitor,500);
};
setTimeout(doMonitor,500);

In theory this should work, but I haven't tested it.

子栖 2024-10-26 21:01:25

您可以利用 DOMSubtreeModified 事件 。当元素的内容发生更改时,该事件会在元素上触发。

$('a').bind('DOMSubtreeModified', function() {
    // contents changed   
});

注意:此事件不会在 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.

$('a').bind('DOMSubtreeModified', function() {
    // contents changed   
});

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/

禾厶谷欠 2024-10-26 21:01:25

a 标签的内容是什么意思?您可以像这样执行此操作:

$('#linkElementID').html();

或者获取像这样的属性:

$('#linkElementID').attr("title"); //Title Attribute 
$('#linkElementID').attr("href"); //href Attribute etc etc 

我不认为标签更改时会触发事件, .change() 事件仅由输入区域触发并选择列表。

您需要在加载新插入内容的事件之后进行检查。

What do you mean content of an a tag? You can do this like so:

$('#linkElementID').html();

or get an attribute like so:

$('#linkElementID').attr("title"); //Title Attribute 
$('#linkElementID').attr("href"); //href Attribute etc etc 

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.

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