[英]Select specific target jQuery
How to make a specific target for tag element, since I want to add some class for the specific i
tag with class icon
, while the parent li
target is collapsed the ul
: 如何为标签元素指定特定的目标,因为我想为带有类icon
的特定i
标签添加一些类,而父级li
目标折叠了ul
:
$('ul li').on('click', function(e) { e.stopPropagation(); $(this).children('ul').slideToggle('slow', function() { // add class to the 'i' tag element }) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <a> <li> <span>Menu</span> <span><i class="icon"></i></span> <ul> <a><li>Sub menu1</li></a> <a><li>Sub menu1</li></a> </ul> </li> </a> <a> <li>Other Menu</li> </a> <a> <li>Other Menu</li> </a> <a> <li>Other Menu</li> </a> <a> <li> <span>Menu</span> <span><i class="icon"></i></span> <ul> <a><li>Sub menu1</li></a> <a><li>Sub menu1</li></a> </ul> </li> </a> </ul>
You can store the clicked <li>
element in a local variable, eg $this
. 您可以将单击的<li>
元素存储在本地变量中,例如$this
。 Then from inside the slideToggle()
callback, you can just re-use it and find()
your <i>
inside. 然后从slideToggle()
回调内部,您可以重复使用它并在内部find()
<i>
。
Also, your closing <a>
tags were wrong, it has to be </a>
, not <a/>
. 另外,您关闭的<a>
标签是错误的,它必须是</a>
而不是<a/>
。 And a <ul>
can only have <li>
elements as direct child nodes. 并且<ul>
只能具有<li>
元素作为直接子节点。 If what you were trieng to achieve by that was merely to have a hand cursor, you can just set cursor: pointer
vias CSS, or have the links directly inside the <li>
s. 如果您要实现的目标仅仅是拥有一个手形光标,则只需设置cursor: pointer
vias CSS,或者直接将链接放在<li>
内部即可。
Working example with a demo class to show it having any effect: 演示类的工作示例展示了它的作用:
$('ul li').on('click', function(e) { e.stopPropagation(); var $this = $(this); $this.children('ul').slideToggle('slow', function() { $this.find('i').addClass('aclass'); }); })
/* test style: */ ul li { cursor: pointer; } ul li i.aclass { border: solid red 2px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li> <span>Menu</span> <span><i class="icon"></i></span> <ul> <a><li>Sub menu1</li></a> <a><li>Sub menu1</li></a> </ul> </li> <li>Other Menu</li> <li>Other Menu</li> <li>Other Menu</li> <li> <span>Menu</span> <span><i class="icon"></i></span> <ul> <a><li>Sub menu1</li></a> <a><li>Sub menu1</li></a> </ul> </li> </ul>
You can save the link that you click and use it later, I think, adding preventDefault
is a good way to prevent scrolling page. 您可以保存单击的链接,以后再使用它,我认为,添加preventDefault
是防止滚动页面的好方法。
$('ul li').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
const $link = $(e.target);
$link.children('ul').slideToggle('slow', function() {
$link.find('i').addClass('aclass');
});
})
You can try this without changing any of your existing code. 您可以尝试此操作,而无需更改任何现有代码。
$('ul li').on('click', function(e) {
e.stopPropagation();
$(this).children('ul').slideToggle('slow', function() {
// add class to the 'i' tag element
$(this).prev().find("span>i.icon").addClass("someclass");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.