[英]Selecting Closest Match using jQuery
I am having trouble selecting the closest match. 我在选择最接近的比赛时遇到麻烦。 I already tried .closest
, .next
, and .nextall
; 我已经尝试过.closest
, .next
和.nextall
; I also tried using (this), but I think I'm using them incorrectly. 我也尝试使用(this),但我认为我使用的方式不正确。
Here's what I want to acheive: When .show
is clicked, the closest .list-content
will toggle and the closest toggleClass icon-rotate-180 too. 这是我要实现的目标:单击.show
,最接近的.list-content
将会切换,最接近的toggleClass icon-rotate-180也将切换。
<ul class="list-unstyled">
<li class="list-menu">
<a href="javascript:void(0);" class="show">
Date of Operations
<i class="icon-chevron-down pull-right"></i>
</a>
</li>
<li class="list-content">Hidden Content until Clicked</li>
<li class="list-menu">
<a href="javascript:void(0);" class="show">
Date of Operations
<i class="icon-chevron-down pull-right"></i>
</a>
</li>
<li class="list-content">Hidden Content until Clicked</li>
</ul>
<script>
$(document).ready(function() {
$(".list-content").hide();
$(".show").click(function() {
$(".icon-chevron-down").toggleClass("icon-rotate-180");
$(".list-content").toggle();
});
});
</script>
Firstly, your HTML is invalid as you cannot have a div
element as a direct child of a ul
. 首先,您的HTML无效,因为您不能将div
元素作为ul
的直接子代。 With that in mind, try this: 考虑到这一点,请尝试以下操作:
<ul class="list-unstyled">
<li class="list-menu">
<a href="#" class="show">
Date of Operations
<i class="icon-chevron-down pull-right"></i>
</a>
<div class="list-content">Hidden Content until Clicked</div>
</li>
<li class="list-menu">
<a href="#" class="show">
Date of Operations
<i class="icon-chevron-down pull-right"></i>
</a>
<div class="list-content">Hidden Content until Clicked</div>
</li>
</ul>
$(".show").click(function(e) {
e.preventDefault();
$(".icon-chevron-down", this).toggleClass("icon-rotate-180");
$(".list-content", $(this).closest('li')).toggle();
});
ul
/ li
is the wrong case here. ul
/ li
在这里是错误的情况。 The second li
is in relation to the first li
. 第二li
与第一li
。 Please use dl
, dd
and dt
instead - this is not the perfect match but better than ul
/ li
! 请改用dl
, dd
和dt
-这不是完美的匹配,但比ul
/ li
!
a
is for links but you dont call a url. a
是用于链接,但您不调用网址。 Please use the dd
itself or a span
so you dont need the javascript:void(0);
请使用dd
本身或span
因此您不需要javascript:void(0);
and no e.preventDefault();
也没有e.preventDefault();
. 。
Date of Operations Hidden Content until Clicked 直到单击为止的隐藏内容的操作日期
<dd class="list-menu show"> Date of Operations <i class="icon-chevron-down pull-right"></i> </dd> <dt class="list-content">Hidden Content until Clicked</dt>
$(".show").click(function(e) { $(this).closest(".icon-chevron-down").toggleClass("icon-rotate-180"); $(this).next("dt.list-content").toggle(); }); $(“。show”)。click(function(e){$(this).closest(“。icon-chevron-down”)。toggleClass(“ icon-rotate-180”); $(this).next( “ dt.list-content”)。toggle();});
(i copied a part of RoyMcCrossan's answer from Selecting Closest Match using jQuery ) (我从使用jQuery选择最接近匹配中复制了RoyMcCrossan的答案的一部分)
Sorry structure broken, looks like a bug in Stackoverflow. 对不起,结构已损坏,看起来像Stackoverflow中的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.