2012-03-11 42 views
1

的一個屬性我定義一個名爲「數據兒」的自定義屬性,使用如:如何判斷HTML的自定義屬性在jQuery的

<div data-children=「test01 test02 test03」></div> 

但我怎麼能判斷酒店「test04」:

var $collection=$('div').attr('data-children') 
……//then what can I do? 

回答

1

由於它返回一個字符串,所以可以使用字符串對象的match方法。如果可能的話,我會建議使用data方法。

if ($('div').data('children').match(/(^|\s+)test04(\s+|$)/)) { 
} 
+0

如果你只是想「它匹配嗎?」行爲與你的正則表達式我建議['.test()'方法](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/test),而不是'.match()'。 – nnnnnn 2012-03-11 04:09:57

相關問題