>> JSFIDDLE <<
var imgFilterBtn = $("nav > ul > li > ul > li > a");
$("img").fadeIn("slow");
imgFilterBtn.click(function() {
var fadeInClass = $(this).attr("id");
var wrongFilter = $(this).parent("li").parent("ul").children("li").children("a");
wrongFilter.removeClass(); // un-style all links in one sub-list
$(this).toggleClass("selected"); // style selected link
var wrongFilterID = wrongFilter.attr("id");
$("#imgWrap").removeClass(wrongFilterID); // remove all classes from imgWrap that are equal to an ID all LI-items in the current list
$("#imgWrap").toggleClass(fadeInClass); // toggle the class that is equal to the ID of the clicked a-element
$("img").hide(); // hide all images
var imgWrapClass = $("#imgWrap").attr("class");
$("#imgWrap").children("img." + imgWrapClass).fadeIn("fast"); // fade in all elements that have the same class as imgWrap
});
スクリプトの動作を説明するコメントを含めるように最善を尽くしました。
1. 機能するもの:
- ドキュメントの読み込み時に画像がフェードインする
- 「選択された」クラスが切り替えられます (ただし、戻るには切り替えられません!)
- クラス
#imgWrap
はオンに切り替えられますが、元に戻りません li
リストアイテム(実際にはその親)がクリックされると、画像が非表示になり、表示されます
2.うまくいかないこと
- li-item をクリックしても、他のクラスは削除されません。
- 上記のこと
3. 何が起こるべきか
ユーザーがリンクをクリックすると、そのリンクの ID が に割り当てられたクラスに渡され#imgWrap
ます。ただし、このクラスが割り当てられる前に、THE SAME LIST (別のリストではない) の他のリスト項目の ID と同じ他のすべてのクラスが削除されます。したがって、 と をクリックするblack
とfashion
、 とが削除されているはずですbrown
#imgWrap
。fashion
brown
black
関数が不足していると推測していeach
ますが、よくわかりません。