2017-03-27 12 views
-1

要素に複数のクラスがある場合(Javascriptの場合)、クラス名を含むすべての要素を取得しようとしています。例:複数のクラスを持つ要素のクラス名を取得する方法1つしか知りません

<div class="one two three four"> 
<div class="one two three"> 
<div class="one two"> 
<div class="one"> 

ここで、クラス「1」を含むすべてのクラスが必要です。したがって、この場合はすべてです。私はgetElementsByClassNameを使用する場合、私は1つのクラスを指定することができますか、そこに正確に複数のクラスを書く必要があります。

すべての提案は本当に感謝しています!

+0

ようにそれを行うことができますか? http://stackoverflow.com/questions/2727303/jquery-counting-elements-by-class-what-is-the-best-way-to-implement-this –

+0

[クラス名で要素を取得する方法?](http://stackoverflow.com/questions/17965956/how-to-get-element-by-class-name) – winter

答えて

0

のは、いくつかのコードを記述し、代わりにSOに尋ねるの直接あなたの質問をテストしようとする方が簡単(かつ迅速)されていると思います。

それともgetElementsByClassName

のマニュアルを読んで[ため息]あなたは、正確な複数のクラスを指定するHACEはありません。実行中:

document.getElementsByClassName("one") 

は4つのdivを持つ配列を与えます。あなたがクラスを持つすべての要素の完全なクラス名を取得したい場合

0

あなたが探しているプロパティはclassNameです。

HTML

<div id="myId" class="one"> 
    stuff 
</div> 
<div id="myId" class="one two"> 
    stuff 
</div> 

JS

var d= document.getElementById('myId'); 
alert(d.className); 
0

何が「1」、それは要素が属するクラスにかかわらず、他のクラスとして「1」を持つすべての要素を返しますする必要がありますしたいです。

document.getElementsByClassName('one') 

ちょうどFYI複数のクラス

document.getElementsByClassName('one two three') 
0

、あなたはこれを試したことがあり、この

// list className of all elements containing the class 'two' 
 
Array.from(document.querySelectorAll('.two')).forEach(function(element) { 
 
    console.log(element.className); 
 
});
<div class="one two three four"> 
 
<div class="one two three"> 
 
<div class="one two"> 
 
<div class="one">

関連する問題