2009-12-16 26 views
1
<img style="cursor: pointer" src="../common/images/plus.jpg"/> View History($revisions) 

如何選擇上面的圖像按鈕?我想選擇與如何使用Jquery選擇一組元素之一?

../common/images/plus.jpg

,當然還有圖像必須是可點擊的源圖像。

我加入

<script> 
$("img[src=../common/images/plus.jpg]").click(function(){ 
    alert("ok"); 
}); 
</script> 

然而,我得到一個錯誤

預期標識符或字符串在屬性選擇 值,但發現 '。

回答

4

如果你真的想選擇由src這應該這樣做:

$('[src=../common/images/plus.jpg]') 

...但你會更好,給它一個ID - 這是更可靠和更快。

<img id="imgPlus" style="cursor: pointer" src="../common/images/plus.jpg"/> View History($revisions) 

然後選擇它,你可以只說:

$('#imgPlus') 
+0

$('[src = ../common/images/plus.jpg]')不起作用。 – Steven 2009-12-16 04:20:31

+1

稍微更靈活的替代方法是$('[src $ =「common/images/plus.jpg」]')。這只是匹配src屬性的後綴,而不是整個事物。 – 2010-05-24 20:08:57

0

最簡單的方法是提供圖像與ID喜歡

<img id="imgClick" style="cursor: pointer" src="../common/images/plus.jpg" /> 

,你可以像這樣的代碼

$("#imgClick").click(function(){ 
}); 
2

使用jQuery你可以使用CSS3 attribu te選擇器找到圖像:

$('img[src="../common/images/plus.jpg"]').click(function() { 
    //handle click event here 
}); 

希望這有助於!

+0

$('[src = ../common/images/plus.jpg]')不起作用。 – Steven 2009-12-16 04:21:46

+0

糟糕。您需要像這樣在URL中添加引號。 $( 'IMG [SRC = 「../公共/圖像/ plus.jpg」]')。看看這篇文章的更多信息:http://forabeautifulweb.com/blog/about/image_management_naming_and_attribute_selectors/ – devongovett 2009-12-17 02:22:22

相關問題