-5

jQueryで特定のクラスを持つすべてのタグのname属性を取得したい。<a>どうすればできますか。

これが私のHTMLだとしましょう:

<div>
    <a href="#" name="first" class="myClass">Something</a>
</div>
<a href="#" name="second">Something else</a>
<a href="#" name="third" class="myClass">Something else again</a>

クラスがあるので、「first」と「third」を取得する必要がありmyClassます。アンカー「サード」にはこのクラスがないため、必要ありません。

4

3 に答える 3

2

クラスセレクター.を使用prop()して属性を取得します。attr()古いバージョンのjquery(jquery 1.6より前)を使用している場合は、これを試してください...

$('.className').prop('name'); //calssName is the name of your class

複数の要素

$('.className').each(function(){
   console.log($(this).prop('name')) 
});

または使用map()

 var nameArray= $('.class').map(function(){
   return this.name;
 }).get();

 console.log(nameArray);  //nameArray is an array with all the names

fiddle ussign マップ

例..

<a name="test" class="class">test</a>

alert($('.class').prop('name'));
于 2013-07-09T07:19:06.233 に答える