在Greasemonkey / javascript中,如何处理添加到页面的新元素?

时间:2021-06-15 21:12:06

I have written a Greasemonkey script which manipulates the contents of certain elements with the following selector:

我编写了一个Greasemonkey脚本,它使用以下选择器操作某些元素的内容:

$("span.relativetime").each(function() { $(this).html("TEST"); });

However, sometimes matching elements are added to the page through AJAX, and I don't know how to handle those new elements. I have tried this, but it doesn't work:

但是,有时匹配元素会通过AJAX添加到页面中,我不知道如何处理这些新元素。我试过这个,但它不起作用:

$("span.relativetime").live(function() { $(this).html("TEST"); });

The documentation for jQuery live() says that it wants an event (like "click"). But I don't have any event, I just want to know when something matching my selector has been created, and then I want to modify it.

jQuery live()的文档说它想要一个事件(比如“点击”)。但我没有任何事件,我只是想知道什么时候匹配我的选择器,然后我想修改它。

Background: I am encountering this problem with a Greasemonkey script to display *'s relative timestamps as absolute local timestamps, which you can find on meta-SO. The problem is when you click "show all comments", the new comments are added by AJAX, and I don't know how to find and replace the timestamps in those scripts.

背景:我遇到这个问题,使用Greasemonkey脚本将*的相对时间戳显示为绝对本地时间戳,您可以在meta-SO上找到它。问题是,当您单击“显示所有注释”时,新注释由AJAX添加,我不知道如何查找和替换这些脚本中的时间戳。

2 个解决方案

#1


0  

With *'s setup I find it annoying to handle stuff after the comments. What I've done is put a bind on the Add/Remove comments button that uses setTimeout to wait for the elements to be created, and then modify them.

使用*的设置,我发现在评论之后处理内容很烦人。我所做的是在添加/删除注释按钮上放置一个绑定,该按钮使用setTimeout等待创建元素,然后修改它们。

#2


0  

One thing you could try (although I'm not sure if it would work) is to cache your selection in some global variable like so:

你可以尝试的一件事(虽然我不确定它是否会起作用)是将你的选择缓存在一些全局变量中,如下所示:

var $relativetime = $("span.relativetime");

Then you would have your .each function:

然后你将拥有你的.each函数:

$relativetime.each(function() { $(this).html("TEST"); });

After your new elements were added to the DOM, you could reselect append to your cached object:

将新元素添加到DOM后,您可以重新选择附加到缓存对象:

$relativetime.append("<my html>"); //or
$("<my html>").appendto($relativetime); 

(P.s. .html() is for setting html. To set text, use .text()

(P.s.html()用于设置html。要设置文本,请使用.text()

#1


0  

With *'s setup I find it annoying to handle stuff after the comments. What I've done is put a bind on the Add/Remove comments button that uses setTimeout to wait for the elements to be created, and then modify them.

使用*的设置,我发现在评论之后处理内容很烦人。我所做的是在添加/删除注释按钮上放置一个绑定,该按钮使用setTimeout等待创建元素,然后修改它们。

#2


0  

One thing you could try (although I'm not sure if it would work) is to cache your selection in some global variable like so:

你可以尝试的一件事(虽然我不确定它是否会起作用)是将你的选择缓存在一些全局变量中,如下所示:

var $relativetime = $("span.relativetime");

Then you would have your .each function:

然后你将拥有你的.each函数:

$relativetime.each(function() { $(this).html("TEST"); });

After your new elements were added to the DOM, you could reselect append to your cached object:

将新元素添加到DOM后,您可以重新选择附加到缓存对象:

$relativetime.append("<my html>"); //or
$("<my html>").appendto($relativetime); 

(P.s. .html() is for setting html. To set text, use .text()

(P.s.html()用于设置html。要设置文本,请使用.text()