jQuery 选择器:如何选择一个类并将其添加到上一个和下一个标签
如何选择一个类并将其添加到上一个和下一个标签。
<div class="sub">
<ul>
<li><a href="#">a</a>
<ul>
<li><a href="#">b</a>
<ul>
<li><a href="#">c</a></li>
<li><a href="#">d</a></li>
<li><a href="#">e</a></li>
</ul>
</li>
<li><a href="#">f</a></li>
<li><a href="#">g</a></li>
</ul>
</li>
<li><a href="#">h</a></li>
<li><a href="#">i</a></li>
<li><a href="#">j</a></li>
</ul>
</div>
例子: 现在我想向相应元素添加 2 个 css 类 (.prev & .next) 假设鼠标位于元素上
g
,所以我想将 2 个类添加到
f
h
片段:
$(document).ready(function() {
$('li').css({'border-top': '1px solid green','border-bottom': '1px solid green'});
$('li a').each(
function(intIndex){
$(this).mouseover(function(){
$(this).css({'border-top': '1px solid red','border-bottom': '1px solid red'});
$(this).prev().find('li').css({'border-bottom': 'none'});
$(this).next().find('li').css({'border-top': 'none'});
}).mouseout(function(){
$(this).css({'border-top': '1px solid green','border-bottom': '1px solid green'});
});
}
);
});
How can I select and add a class to the prev and next a tag.
<div class="sub">
<ul>
<li><a href="#">a</a>
<ul>
<li><a href="#">b</a>
<ul>
<li><a href="#">c</a></li>
<li><a href="#">d</a></li>
<li><a href="#">e</a></li>
</ul>
</li>
<li><a href="#">f</a></li>
<li><a href="#">g</a></li>
</ul>
</li>
<li><a href="#">h</a></li>
<li><a href="#">i</a></li>
<li><a href="#">j</a></li>
</ul>
</div>
example:
now i want to add 2 css classes (.prev & .next) to the the according elements presume that the mouse is over the element <li><a href="#">g</a></li>
, so i want to add the 2 classes to <li><a href="#">f</a></li>
and <li><a href="#">h</a></li>
snip:
$(document).ready(function() {
$('li').css({'border-top': '1px solid green','border-bottom': '1px solid green'});
$('li a').each(
function(intIndex){
$(this).mouseover(function(){
$(this).css({'border-top': '1px solid red','border-bottom': '1px solid red'});
$(this).prev().find('li').css({'border-bottom': 'none'});
$(this).next().find('li').css({'border-top': 'none'});
}).mouseout(function(){
$(this).css({'border-top': '1px solid green','border-bottom': '1px solid green'});
});
}
);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个: http://jsfiddle.net/rsEaF/1/
编辑:< /strong> 一项小修正。以前,如果您将鼠标悬停在第一项上,
.prev
类将添加到列表中的最后项。此问题已得到纠正。编辑:这是一个使用
.each()
分配处理程序的版本。这样做的效率会更高一些,因为它不需要为每个mouseenter
调用$a.index(this);
。尝试一下: http://jsfiddle.net/rsEaF/3/
编辑:如果您对持续删除正确的类有任何问题,请删除这行代码:
并在处理程序的开头,替换它与:
或:
Try this: http://jsfiddle.net/rsEaF/1/
EDIT: One minor correction. Previously, if you hovered over the first item, the
.prev
class would be added to the last item in the list. This has been corrected.EDIT: Here's a version that uses
.each()
to assign the handler. Doing it this way is a little more efficient because it removes the need to call$a.index(this);
for eachmouseenter
.Try it out: http://jsfiddle.net/rsEaF/3/
EDIT: If you're having any issue with the proper class being removed consistently, get rid of this line of code:
and at the beginning of the handler, replace it with:
or:
您应该努力避免像这样附加事件侦听器
附加像这样的侦听器就足够了(并且更有效):
此外,jQuery 1.3 还引入了
.live()
(请参阅 http://api.jquery.com/live/)与选择器匹配的元素抛出的任何事件,无论是否存在当
.live()
被调用或稍后创建时,将被传递给指定的函数。You should make an effort to avoid attaching event listeners like this
It is sufficient (and more efficient) to attach a listener like so:
Also jQuery 1.3 introduces
.live()
(see http://api.jquery.com/live/)Any event thrown by an element that matches the selector, whether it exists when
.live()
is called or is created later, will be passed to the specified function.这应该有效。但它会循环遍历整个
.sub
div,因此如果该列表变得非常长,它可能会效率不高。This should work. It loops through the entire
.sub
div though, so it may not be efficient if that list can get really long.