これは、多くの方法を達成することができます。私はそのようにして、ページ内で使われているすべての種類のクラスを尋ねます。ループ内でそれらを使って要素にアクセスするのは簡単でした。そしてそれは本当にあなたがそれらと何をしたいのかに主観的です。
私のコードが行く:ここ
<a href="#" class="title25">Some text1</a>
<figcaption class="title25">Some text2</figcaption>
<div class="title30">Some text3</div>
<span class="title30">Some text4</span>
<p class="title50">Some text5</p>
<ul class="title50">
<li>Some text6</li>
</ul>
例はjQueryを使って示されています。また、生のJavaScriptやその他のフレームワークを使用することもできます。
var classes = [];
$('[class]').each(function(){
$($(this).attr('class').split(' ')).each(function() {
if (this.length>0 && $.inArray(this.valueOf(), classes) === -1) {
classes.push(this.valueOf());
}
});
});
console.log(classes);
classes.forEach(function(i){
$('.'+i+':first').attr("style", "color: red;");
$('.'+i+':last').attr("style", "color: green;");
$('DIV.'+i).attr("style", "font-size: 24px;");
$('p.'+i).attr("style", "font-weight: 800;");
$('span.'+i).attr("style", "text-decoration: underline;");
});
JSFiddleリンク:https://jsfiddle.net/27g7aayt/1/
あなたは要素の名前を知っているか、ないのですか? 「a」、「figcaption」? –
はい、 'a'と' figcaption'は私がターゲットにする必要のある要素です。 – viery365