2017-03-05 49 views
1

爲了讓我的腳本更加耐用 無法檢測 健壯,我決定使用更多的人類事件,即mouseover,mousedown,mouseup,mouseout。由於這些是jQuery的依賴,我已經加入jQuery + Greasmonkey:mouseover()不是函數

// @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"); 

回答

2

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/

+0

尼斯快速回答!謝謝! –