私はまだ JQuery と JavaScript 全体にかなり慣れていないので、ご容赦ください。質問に対する答えをウェブで検索して、少し実験してみましたが、うまくいきません。とにかく、 JavaScriptで複数のCSSクラスを配列に格納する方法はありますか?
次のような友人のポートフォリオ Web サイト用の単純な JQuery を作成しています。
$('.item.two').click(function(){
$('.content.item, .current.item').show();
$('.content.item, .content.item, .content.item, .current.item, .current.item, .current.item').hide();
$('.item.one, .item.three, .item.four').fadeTo(0, 0.5);
$('.item.two').fadeTo(0, 1.0);
});
これは、特定の要素を非表示にして、対応するアイコンがクリックされたときにのみ表示するだけです。これはまた、クリックすると不透明度がメイン クラスの 50% オフから 100% に変わります。
コード自体に問題はなく、意図した目的を果たしています。しかし、これらのクラスを再利用可能な配列に保持するだけで、このコードを少しきれいにする方法はありますか? 私はそれが可能であるべきだと感じていますが、方法がわかりません。
ありがとう!
EDIT : 申し訳ありませんが、実際に 4 つの異なるクラスを使用して非表示または表示していることは明確ではありませんでした。したがって、前のビットの代わりに、実際には
$('.item.two').click(function(){
// this is the content i want to show on click
$('.content.itemTwo').show();
// this is the content that i want to hide/remain hiding on click
$('.content.itemOne, .content.itemThree, .content.itemFour').hide();
// these are icons representing the content
$('.item.one, .item.three, .item.four').fadeTo(0, 0.5);
$('.item.two').fadeTo(0, 1.0);
});
また、これは私の HTML です。私が言ったように、私が実現しようとしていることは起こります。それを実現するためのより良い方法があるように感じます。
<!-- these are icons representing the written content-->
<div class="item one">
<div class="fs1" aria-hidden="true" data-icon=""></div>
</div>
<div class="item two">
<div class="fs1" aria-hidden="true" data-icon=""></div>
</div>
<div class="item three">
<div class="fs1" aria-hidden="true" data-icon=""></div>
</div>
<div class="item four">
<div class="fs1" aria-hidden="true" data-icon=""></div>
</div>
<!-- this is the written content to be shown upon clicking corresponding icon -->
<div class="content itemOne">
<h3>itemOne</h3>
<p>....</p>
</div>
<div class="content itemTwo">
<h3>itemTwo</h3>
<p>...</p>
<div class="content itemThree">
<h3>itemThree</h3>
<p>...</p>
</div>
<div class="content itemFour">
<h3>itemFour</h3>
<p>....</p>
</div>
今見てみると、おそらく .content または .item に追加のセレクターは必要ありません。