javascript - jQuery + Greasmonkey : mouseover() is not a function

标签 javascript jquery greasemonkey

为了使我的脚本更加不可检测 健壮,我决定使用更多的人工事件,即 mouseover、mousedown、mouseup、mouseout。由于这些依赖于 jQuery,因此我添加了

// @require      https://code.jquery.com/jquery-3.1.1.slim.min.js

到我的脚本的顶部,并且按照 Greasmonkey Wiki 中的说明包括这一行:

this.$ = this.jQuery = jQuery.noConflict(true);

当我运行这个脚本时,我在控制台中得到这个错误:

Uncaught TypeError: document.getElementsByClassName(...)[0].mouseover is not a function

这会让我相信 jQuery 尚未加载到脚本安装中。我在没有“noConflict”的情况下尝试了相同的代码,它有效地破坏了网站,并且仍然返回上述错误。

这是我的代码:

else if (document.getElementsByClassName('thumb-container active')[0] == null &&
         document.getElementsByClassName('feed-ajax-next')[0] != null){
         document.getElementsByClassName('feed-ajax-next')[0].mouseover();
         console.log("M_over");
         document.getElementsByClassName('feed-ajax-next')[0].mousedown();
         console.log("M_down");
         document.getElementsByClassName('feed-ajax-next')[0].mouseup();
         console.log("M_up");
         document.getElementsByClassName('feed-ajax-next')[0].mouseout();
         console.log("M_out");

最佳答案

document.getElementsByClassName() 不返回 jQuery 对象,但 mouseover() 和其他函数是 jQuery 函数。

在这种情况下,您需要使用 jQuery('.feed-ajax-next') 将元素作为 jQuery 对象获取,然后使用这样的函数 - jQuery( '.feed-ajax-next').mouseover()

您可以在此处阅读有关 jQuery 对象的更多信息:https://learn.jquery.com/using-jquery-core/jquery-object/

关于javascript - jQuery + Greasmonkey : mouseover() is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42614061/

相关文章:

javascript - 跟踪 Google Analytics 然后重定向..?

javascript - 未捕获的异常错误 "Illegal operation on WrappedNative prototype object"

jquery - 从pdf和word转换为图像

javascript - 如何在没有元素 ID 的下拉列表中使用 Javascript 选择选项?

javascript - 编辑javascript文件时eclipse停止工作

javascript - 如何在 gethash javascript 函数中查找具有特定字母的字符串

jquery - 第一个按钮有一个功能,第二个按钮有另一个功能

javascript - Greasemonkey 按钮点击

javascript - 使用 JavaScript 和 tampermonkey 更改网站中的文本

javascript - 如何在分布式环境中处理 PostgreSQL 触发器