使Greasemonkey对元素的Ajax变化做出反应 [英] Make Greasemonkey react to ajax change of an element
问题描述
有一个页面加载时像这样的空跨度:<span id="bla"></span>
,后来又用一些文本填充该跨度.
There is a page that loads with an empty span like this: <span id="bla"></span>
and later fills that span with some text.
我需要我的脚本来获取文本,但是Greasemonkey会在文本填充之前运行,甚至我尝试使用的waitForKeyElements
函数也无济于事,因为加载页面时该元素已经存在.
I need my script to get that text but Greasemonkey runs before it is filled with the text and even the waitForKeyElements
function, that I tried to use, is not helping because the element is already there when the page loads.
也许我需要另一种方式?目前我正在做:
Maybe I need in another way? Currently I am doing:
waitForKeyElements ("#bla", get_span_content)
推荐答案
由于您已经在使用 waitForKeyElements ,使用操作功能的返回值微调结果.如果跨度仅填充/更改一次,则代码将如下所示:
Since you are already using waitForKeyElements, use the action-function's return value to fine tune the results. If the span is only filled/changed once, the code would look like this:
waitForKeyElements ("#bla", get_span_content);
function get_span_content (jNode) {
var spanText = $.trim (jNode.text () );
if (spanText == "") {
//-- Still blank; tell waitForKeyElements to keep looking.
return true;
}
else {
// DO WHATEVER WITH spanText HERE.
}
}
如果相同跨度被更改多次,则代码将如下所示:
If the same span is changed multiple times, the code would look like this:
waitForKeyElements ("#bla", get_span_content);
function get_span_content (jNode) {
var spanText = $.trim (jNode.text () );
var lastText = jNode.data ("lastText") || "";
if (spanText != lastText) {
// DO WHATEVER WITH spanText HERE.
jNode.data ("lastText", spanText);
}
return true;
}
这篇关于使Greasemonkey对元素的Ajax变化做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!