我有一個鏈接,我試圖模擬點擊。我正在使用下面的代碼,但執行腳本時沒有任何反應。有任何想法嗎?如何用jquery點擊一個鏈接包含
代碼
$("a:contains('somewebsite')").click();
鏈接
<a href="http://somewebsite.com/radio.php">Radio</a>
我有一個鏈接,我試圖模擬點擊。我正在使用下面的代碼,但執行腳本時沒有任何反應。有任何想法嗎?如何用jquery點擊一個鏈接包含
代碼
$("a:contains('somewebsite')").click();
鏈接
<a href="http://somewebsite.com/radio.php">Radio</a>
contains
檢查文本內容。你想要一個attribute contains
選擇:
$("a[href*='somewebsite']").click();
你會好起來的分配類或ID的鏈接。它會更簡單,更快捷。
<a id="webLink" href="http://somewebsite.com/radio.php">Radio</a>
腳本:
$("#webLink").click();
是否有一個原因,這種方法不會對您最好的嗎?
你想
$('a[href*="somewebsite"]').click();
代替。正如你使用它,它檢查標籤的文本內容,而不是屬性。
嘗試
$("a[href*='somewebsite']").click();
:contains()將檢查文本值,在這種情況下將是「廣播」的內容。因此,這會工作:
$("a:contains('Radio')").click();
你後,更可能的是什麼,是使用attribute contains selector:
$("a[href*='somewebsite']").click();
這將單擊其中的href([HREF])屬性,它包含一個鏈接([href * =「」])somewebsite。
You linked:contains和attribute contains selector to same thing,but said they are different ... – ClarkeyBoy 2012-01-15 21:12:20
Grr,你是對的。固定 – Keithamus 2012-01-26 21:04:24