2012-03-01 59 views

回答

6

你可以這樣做:

if ($('#selection').attr('title')){ 
alert('YAY 4 TITLES'); 
} 
+0

d你呢,打我5秒! – Alex 2012-03-01 11:24:29

0
var img = $("#idOfYourImage"); 
alert(img.is("[title]"); 

希望它能幫助:)

你也可以只包括[title]屬性時尋找圖像。

alert($("#idOfYourImage[title]").length > 0); 
0
if ($('#img').attr('title') !== undefined) { 
    // attribute exists 
} else { 
    // attribute does not exist 
} 
+0

Err ..好吧,爲什麼downvote? – Tx3 2012-03-01 11:55:20

0

要檢查是否<img>標籤已經title屬性設置:

if($("img#someID").attr("title") !== undefined) { 
    // .... 
} 

要選擇有title屬性圖片:

$("img[title]") 

注:

  • 由於jQuery的1.6 attr()方法返回undefined如果屬性不存在和""如果屬性是存在的,但空
  • 在jQuery中電子arlier版本的attr()方法返回""如果屬性不存在,或者它是存在的,但空
+0

什麼是-1的亂舞? – 2012-03-01 11:52:09

1

像這樣我的朋友:

if($('element').attr('title')){ 
    // do something 
} 
0
if($("#imgID").attr("title")==undefined || $("#imgID").attr("title")!=""){ 

Do Something 

} 
1
if($("img").attr("title")){ 
    // do something 
} 

這將做到這一點:)

我喜歡它的,所以當一些簡單的問題是問所有的答案都搶着在:d

2
如果 img標籤有一個title屬性

給你img標籤的ID

if($("#imgID").is("[title]")){ 
//your code here 
} 

選擇img標記出具有標題attr可以使用 所有img標籤下面的代碼會過濾掉所有的IMG標記,具有attr標題並隱藏它們,您可以根據自己的需要進行修改

$("img").filter(function(){return $(this).is("[title]")}).hide(); 
+2

或者,你可以只寫'$(「img [title]」)。hide();' – 2012-03-01 11:51:31