2011-10-27 90 views
0

我有一套的div例如:jQuery的屬性包含選擇

<div class="wrapper"> 
<div class="uct_attr" data-tags="one two three four">stuff</div> 
<div class="uct_attr" data-tags="three four">stuff</div> 
<div class="uct_attr" data-tags="one three five">stuff</div> 
<div class="uct_attr" data-tags="one two six">stuff</div> 
<div class="uct_attr" data-tags="four five six">stuff</div> 
</div? 

如何執行和行動上符合我上不的那些查詢,另一種的人?記住我的願望是匹配的屬性數據標籤

例如,一家之言:

// if match 'one' 
$('.wrapper .uct_attr[data-tags="one"]').doMatch(); 
// the rest that didn't match 'one' 
$('.wrapper .uct_attr[data-tags!="one"]').doNotMatch(); 

的問題,我認爲是存在這樣的屬性multipul標籤。

對此的任何幫助將是偉大的。

Ç

+1

你是如此接近:http://api.jquery.com/attribute-contains-選擇器/ –

+0

這對''=' – Cybercampbell

+0

是如何工作的請參閱我的答案以瞭解如何取消 –

回答

1

很簡單:使用member-of-a-whitespace-delimited-list選擇。

var $els = $(".wrapper .uct_attr"); 
$els.filter("[data-tags~='one']").doMatch(); 
$els.not("[data-tags~='one']").doNotMatch(); 
+0

完美謝謝!但是如果我想匹配一個多標籤: 例如:'[data-tags〜='one three six']'其中任何一個都可以匹配。 – Cybercampbell

+1

'[data-tags〜=「one」],[data-tags〜=「three」],[data-tags〜=「six」]'。 CSS並不總是簡潔。 ;) – kojiro

0

沒有選擇器

$('.wrapper').find('div').not('.uct_attr[data-tags="one"]').doNotMatch(); 
+0

它應該是:not() –

+0

':not'是CSS選擇器。還有'.not()'jquery方法。 – kojiro

+1

您仍在使用*屬性equals *選擇器。 O.P.的問題是關於匹配以空格分隔的列表的成員。 – kojiro

0

要在屬性匹配一個單詞,使用〜=操作如下定義:http://api.jquery.com/attribute-contains-word-selector/

// if match 'one' 
$('.wrapper .uct_attr[datatags~="one"]').doMatch(); 
// the rest that didn't match 'one' 
$('.wrapper .uct_attr').not('[datatags~="one"]').doNotMatch(); 
+0

「data-」是W3C對非標準html屬性名稱的要求。 – moka

+0

@MaksimsMihejevs好點。我編輯了我的答案。 –