使用`waitForKeyElements`和JQuery`.on()`有什么区别?

问题描述 投票:-1回答:3

waitForKeyElements和JQuery .on()之间有什么区别?

类似下面的[[adapted示例,来自https://stackoverflow.com/a/53839921/982924

// ==UserScript== // @name _delete Adblock blocking nodes // @match *://YOUR_SERVER.COM/YOUR_PATH/* // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @grant GM_addStyle // @grant GM.getValue // ==/UserScript== //- The @grant directives are needed to restore the proper sandbox. /* global $, waitForKeyElements */ waitForKeyElements ("[id$='adblockinfo']", killNode); function killNode (elem) { elem.remove(); }
但是我通常会做类似的事情

// ==UserScript== // @name _delete Adblock blocking nodes // @match *://YOUR_SERVER.COM/YOUR_PATH/* // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @grant GM_addStyle // @grant GM.getValue // ==/UserScript== //- The @grant directives are needed to restore the proper sandbox. /* global $, waitForKeyElements */ elem = $('#killme') $(document).on('load ready', elem, function() { $(this).remove(); });

鉴于作者在javascript中的经验[https://stackoverflow.com/users/331508/brock-adams,他当然知道他在做什么。但我仍然想了解这两种技术之间的区别。


给出名称和用法,waitForKeyElements将会...好吧...等等(并等待)直到这些元素可用。而你的可以通过setTimeout或ajax加载轻松绕过doc.on(ready)。此外,waitForKeyElements会在DOM中轮询元素(每个元素距来源300ms),而.on(load)将立即触发。 –自由度-m

随时对此进行纠正:我可以通过删除选择器来模仿此行为,因此将监视整个文档及其后代。

原状

elem = $('#killme') $(document).on('load ready', function() { elem.remove(); });

这就是我从https://api.jquery.com/on/中了解的内容>

如果选择器被省略或为空,则事件处理程序称为直接或直接绑定。每当事件发生在选定元素上时,无论事件是直接发生在元素上还是来自后代(内部)元素的气泡,都将调用处理程序。

使用waitForKeyElements和JQuery .on()有什么区别?就像下面的改编示例一样,来自https://stackoverflow.com/a/53839921/982924 // == UserScript == // @name _delete ...
javascript jquery greasemonkey tampermonkey userscripts
3个回答
1
投票
如果您查看waitForKeyElements的来源,您会看到它是如何工作的。

0
投票

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.