脚本尽快元素存在触发一个事件?脚本、元素、存在、事件

2023-09-10 14:07:54 作者:阳光下的白鸽

我试图写一个小的Greasemonkey脚本实现某些任务的Facebook,喜欢躲在新闻等。

I'm trying to write a little Greasemonkey script for achieving some tasks in Facebook, like hiding news and such.

的问题是:我具有ID到未现有尚未在DOM的元素。这是击中后的箭头图标时,屏幕上出现的小盒子。

The problem is: I'm having an ID to an element which isn't existing yet in the DOM. It's the little box which appears when hitting a post's arrow-icon.

如何创建通过jQuery的事件处理程序而一旦触发的元素存在?

How can I create an event handler through jQuery which fires as soon as the element exists?

推荐答案

这是一个常见的​​问题。您可以在行动AJAXed-中的元素,因为它们出现使用的 waitForKeyElements()实用。 (见 例如火的Greasemonkey在AJAX请求脚本。)

This is a common problem. You can act on AJAXed-in elements as they appear by using the waitForKeyElements() utility. (See "Fire Greasemonkey script on AJAX request" for example.)

我不知道你所说的击中后的箭头图标时,屏幕上出现的小盒子的意思,但在一般情况下,你会:

I'm not sure what you mean by "the little box which appears when hitting a post's arrow-icon", but in general, you would:

请确保您的脚本有jQuery的。这种添加到元数据部分,如果它不存在的话:

Make sure your script has jQuery. Add this to the metadata section, if it's not there already:

// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js

要求,在 waitForKeyElements

// @require https://gist.github.com/raw/2625891/waitForKeyElements.js

写您的处理函数,并将其激活:

Write your handler function and activate it:

function hideTheNews (jNode) {
    //***** YOUR CODE HERE *****
    jNode.hide (); // For example
}

waitForKeyElements ("*APPROPRIATE jQuery SELECTOR*", hideTheNews);