2016-09-07 63 views
0

我在寫擴展名爲chrome,我需要自動點擊下一頁的鏈接。
我用jQuery.noConflict();在我的Chrome控制檯總是拋出的錯誤 - jquery-3.1.0.min.js:2 Uncaught TypeError: target.dispatchEvent is not a function.
這是我的代碼:模擬鼠標點擊鏈接進行Chrome擴展

var dispatchMouseEvent = function (target, var_args) { 
var e = document.createEvent("MouseEvents"); 
e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1)); 
target.dispatchEvent(e);}; 

function GoToNextPage() { 
var link_nextpage = null; 
jQuery(function ($) { 
    var pages = $('#results-pagination'); 
    var next_page = pages.find('.next'); 
    var link_nextpage = null; 
    if (next_page.length) { 
     link_nextpage = next_page.find('a'); 
     dispatchMouseEvent(link_nextpage, 'click', true, true); 
    }  
}); 
} 

能否請你解釋一下,如果有可能做到這一點,或者告訴我做錯了什麼?

+1

link_nextpage是一個jQuery對象,而不是一個DOM元素。請使用.trigger或link_nextpage [0],更重要的是:調試您的代碼! – wOxxOm

+0

link_nextpage [0]的作品,感謝您的幫助。 –

回答

0

正如所說的wOxxOm,link_nextpage是一個jquery對象,而不是一個dom元素,dispatchMouseEvent(link_nextpage [0],'click',true,true);很好。
這是我愚蠢的錯誤。