2013-01-11 60 views
-1

這裏有什麼問題;它不點擊?Greasemonkey腳本未點擊按鈕

我的代碼:

// ==UserScript== 
// @name  premiumleech 
// @namespace premiumleech 
// @include  http://generator.premiumleech.com/link/*.html 
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js 
// @grant GM_addStyle 
// ==/UserScript== 
/*- The @grant directive is needed to work around a design change 
introduced in GM 1.0. It restores the sandbox. 
*/ 

/* 
<p class="leech-btn"><button onclick="javascript:window.open('http://generator.premiumleech.com/redir/BtPpnGJv2HcZRa2BTxsEQzUdDQbb6WO1','_blank');">Leech it!</button></p> 
*/ 
/* 
/html/body/div[2]/div/section/p[2] 
*/ 

var targBtn = $("#content > p.leech-btn > button"); 
if (targBtn.length) { 
targBtn[0].click(); 
} 


我試過JavaScript的第一次,但沒能適應它爲queryselector「無法找到該按鈕Weired任何單詞」,並get..ByTagName失敗,在沒有明顯的原因至少對我來說

function clickc() 
{ 
var el = document.querySelector("_blank"); or document.getElementsByTagName("TABLE"); 
var evt = document.createEvent("MouseEvents"); 
event.initMouseEvent("click", true, true, window, 
      0, 0, 0, 0, 0, 
      false, false, false, false, 
      0, null); 
el.dispatchEvent(evt); 
} 
+0

'.click()'應該可以正常工作。你是從那個'$(「body> div:second> div:first> div:first> p:second」)''獲得一個元素嗎? – Cerbrus

回答

1

jQuery的click()方法不會觸發元素的點擊操作。它會觸發爲click事件處理程序註冊的操作,如果傳遞參數,則將其用作點擊該元素的回調函數。點擊該HTML元素你需要使用

$("body > div:second > div:first > div:first > p:second").get(0).click(); 

這將基本上選擇HTML元素(而不是jQuery對象)並觸發點擊事件。

+0

沒有工作我測試的鏈接是這樣的 http://generator.premiumleech.com/link/qBtrxQmWvMr9y7XbcPSv87qNsbpjDVbs.html –