2014-08-29 57 views
0
<script> 
$("div:contains('John')").css("text-decoration", "underline"); 
</script> 

幾乎我想運行一個函數,而不是改變div的CSS屬性。 例如,如果有一個名稱包含約翰執行$(selector).click();檢查文本是否存在,然後運行功能

任何想法?

+2

if語句不會做? – karthikr 2014-08-29 14:44:16

+0

[jQuery:檢查文本是否存在於另一個元素中]的可能重複(http://stackoverflow.com/questions/9300123/jquery-check-if-text-exists-in-another-element) – ctwheels 2014-08-29 15:09:27

回答

2

要檢查所選元素存在獲得length財產,並確保它比0更多:

if ($('div:contains("John")').length) { 
    // element exists 
} 
+0

謝謝VisioN我不確定關於我正在嘗試的語法 – cppit 2014-08-29 14:46:50

2

檢查DIV長度不能爲空

if ($('div:contains("John")').length>0) { 
    // your script here 
} 
相關問題