Javascript 根据所选li显示相关li
我有一份清单:Javascript 根据所选li显示相关li,javascript,jquery,html-lists,show-hide,Javascript,Jquery,Html Lists,Show Hide,我有一份清单: <ul class="products"> <li class="products p1">first</li> <li class="description p1">first</li> <li class="products p2">second</li> <li class="description p2">second</li> <li c
<ul class="products">
<li class="products p1">first</li>
<li class="description p1">first</li>
<li class="products p2">second</li>
<li class="description p2">second</li>
<li class="products p3">third</li>
<li class="description p3">third</li>
<li class="products p4">fourth</li>
<li class="description p4">fourth</li>
</ul>
它很好用。现在我想显示活动li的相关说明。
例如,如果活动li是类为p2的li,则需要显示以下描述li,并隐藏其他li:
<li class="description p2">second</li>
第二个
说明li在开头隐藏。我不知道如何根据类名选择正确的li。应该有一个jQuery技巧,通过带掩码的类名来选择li 将
.active
类添加到下一个li
($(this).next().addClass(“active”);
),并将选择器更改为li.products
,因为所有li
都可以单击:
$(window).load(function(){
$(".products li.products").click(function(){
if ($(this).hasClass("active")) {
$(".products li").show("slow");
$(this).removeClass("active");
$(this).next().removeClass("active");
} else {
$(this).addClass("active");
$(this).next().addClass("active");
$(".products li:not(.active)").hide("slow");
}
});
});
将
.active
类添加到下一个li
($(this).next().addClass(“active”);
),并将选择器更改为li.products
,因为所有li
都可以单击:
$(window).load(function(){
$(".products li.products").click(function(){
if ($(this).hasClass("active")) {
$(".products li").show("slow");
$(this).removeClass("active");
$(this).next().removeClass("active");
} else {
$(this).addClass("active");
$(this).next().addClass("active");
$(".products li:not(.active)").hide("slow");
}
});
});
尝试使用.next()
jQuery函数。由于描述始终是活动元素的下一个li
元素,因此您应该能够获得下一个同级元素并使用.show()
.next():
Get the immediately following sibling of each element in the set of matched elements. If a
selector is provided, it retrieves the next sibling only if it matches that selector.
尝试使用.next()
jQuery函数。由于描述始终是活动元素的下一个li
元素,因此您应该能够获得下一个同级元素并使用.show()
.next():
Get the immediately following sibling of each element in the set of matched elements. If a
selector is provided, it retrieves the next sibling only if it matches that selector.