我有以下代碼:jQuery attr似乎不返回字符串?
JS:
var test2 = ['RMrpi5Z8doc','JIPbUaqyYx0','MbjXYg0YRmw'];
$('tr').live('click', function(event){
$($(this).attr('class').split(' ')).each(function() {
if (!((this == 'even') || (this == 'odd'))) {
alert(jQuery.inArray(this, test2));
if (this == 'RMrpi5Z8doc') {
alert(this);
}
}
});
});
HTML:
<table>
<tr class="odd RMrpi5Z8doc">
<td>Kite</td>
<td>Just Like Vinyl</td>
<td>Audiotree</td>
</tr>
</table>
inArray不匹配,並返回-1。與文字字符串匹配的if語句匹配。如果我用inArray中的文字替換,那也匹配。
我見過一篇文章說jQuery attr不會再返回字符串,但是查看jQuery網站上attr的文檔似乎是這樣說的。
也許我應該以完全不同的方式來談論這個?
你想達到什麼目的? –
*「也許我應該採取完全不同的方式?」*我建議不要使用class來存儲這個值。你可以使用'data-id',或者如果它們是唯一的,'id'應該可以工作。 –
雖然您正在考慮重新編寫代碼,但請注意'.live()'已被棄用一段時間了。如果你使用最新的jQuery,你應該使用'.on()'API。 – Pointy