>>JSFIDDLE< <与同班的父亲,这是相同的列表项的ID
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
类翻转(但不BACK切换!) ,但不会回拨 - 图像被隐藏并显示何时列表项(实际上是其父项
li
)被点击
2.什么不起作用
- 当单击李项,其他类不被删除上述
提到
3.应该怎么办 当用户点击链接时,链接的ID将被传递给分配给#imgWrap
的类。但在分配此类之前,所有其他类与“同类列表”的其他列表项的标识相同的其他类(不包含另一列表)将被删除。因此,当您点击black
和fashion
,然后brown
#imgWrap
应具有类别fashion
和brown
,black
应已被删除。
我猜我错过了each
函数,但我不确定。
我已经想到,这是一个问题(全部被选中)。虽然我不会为#imgWrap的类的结论!好想法。有没有可能有一些链接可以很好地解释正则表达式?我真的不知道他们很多,但很想了解更多。我会尽快将您的答案标记为正确!哦,还有另一个问题:if表达式实际上会问什么?因为我不明白为什么会这样。再次感谢! – 2012-07-09 17:05:58
对于正则表达式,我推荐http://www.regular-expressions.info/。关于if语句:当组中没有选择菜单项时,'wrongFilter'将是一个空集,因此'wrongFilter.attr(“id”)'将返回'undefined',因为它不能从ID中获得ID任何元素。但是将'undefined'传递给'.removeClass'与传递没有参数相同,否则这些参数将删除该元素的所有现有类。这不是你想要的。例如。如果选择了'时尚'和后来的'黑色',没有'if',它会将'fashion'移除为类,因为之前没有选择颜色。 – 2012-07-09 23:12:34
if(wrongFilterID)只是确保'wrongFilterID'实际上包含一个非空字符串,可以从类列表中删除。 – 2012-07-09 23:13:14