假設有三類的JavaScript得到的類名
<div class="hello world_1"></div>
<div class="hello world_2"></div>
<div class="hello world_3"></div>
我想要得到的類名「world_1」,「world_2」,並根據「你好」「world_3」。代碼:
$('.hello').each(function(){
this.attr('class');
});
了錯誤說:
TypeError: Object #<HTMLDivElement> has no method 'attr'
我嘗試一點點,發現
$('.hello:first').attr('class')
作品而
$('.hello')[0].attr('class')
引發上述錯誤。
任何人都可以解釋爲什麼發生了,我怎麼能得到我想要的?
感謝
或'this.className'。 – RobG
@alex你能說一下,爲什麼'/(?:^ | \ s)world_ \ d +(?:^ | \ $)/'([根本不起作用](http://jsfiddle.net/) J22Ex /))而不是簡單的'/ world_ \ d + /'? – Engineer
@Engineer這對我來說是一個巨大的失敗。我會解決它。 – alex