[英]jQuery disable the previous span and the next span, on click span
I'm having trouble applying class to the previous element or the next element. 我在将类应用于上一个元素或下一个元素时遇到麻烦。
<a href="#"><span class="glyphicon glyphicon-thumbs-up up"></span>
<a href="#"><span class="glyphicon glyphicon-thumbs-down down"></span>
I want to disable the second span, when I click the first span and vice versa. 当我单击第一个跨度时,我想禁用第二个跨度,反之亦然。
$(document).on('click','.up, .down',function (e){
e.preventDefault();
if ($(this).hasClass('up')){
var down = $(this).parent().nextAll('a.comment-vote-down');
down.removeClass('disable-link').addClass('disable-link');
}else if($(this).hasClass('down')){
var up = $(this).parent().prevAll('a.comment-vote-up');
up.removeClass('disable-link').addClass('disable-link');
}
});
You try to find class .comment-vote-down
that doesn't exist in your HTML code 您尝试找到HTML代码中不存在的类.comment-vote-down
https://jsfiddle.net/xnovq390/4/ https://jsfiddle.net/xnovq390/4/
try this: 尝试这个:
$(document).on('click','.up, .down',function (e){
e.preventDefault();
if ($(this).hasClass('up')){
var down = $(".down");
down.removeClass('disable-link').addClass('disable-link');
}else if($(this).hasClass('down')){
var up = $(".up");
up.removeClass('disable-link').addClass('disable-link');
}
});
It's because you don't have an a with a class of .comment-vote-down, or comment-vote-up. 这是因为您没有带有.comment-vote-down或comment-vote-up类的a。
Either take that part out of your nextAll selection: 从您的nextAll选择中删除该部分:
.nextAll('a');
https://jsfiddle.net/xnovq390/1/ https://jsfiddle.net/xnovq390/1/
or, add in the class to your HTML: 或者,将该类添加到您的HTML中:
<a href="#" class="comment-vote-up">
https://jsfiddle.net/xnovq390/3/ https://jsfiddle.net/xnovq390/3/
If the .up
element is always the previous element from the .down
one, you can simply do: 如果.up
元素总是从先前元素.down
一个,你可以简单地做:
$(document).on('click', '.up, .down', function(e) {
e.preventDefault();
if ($(this).hasClass('up')) $(this).next().addClass('disable-link');
else $(this).prev().addClass('disable-link');
}
$(document).on('click','.up, .down',function (e){
e.preventDefault();
if ($(this).hasClass('up')){
$(this).addClass('disable-link');
$('span.down').removeClass('disable-link');
}else if($(this).hasClass('down')){
$(this).addClass('disable-link');
$('span.up').removeClass('disable-link');
}
});
As the other answers already stated, there is no spoon element with .comment-vote-down
class. 如其他答案所述, .comment-vote-down
类没有汤匙元素。 But additionally to make life easier, you could buffer the 2 elements, bind the click to them and disable the ones not clicked, reducing the code to 但除此之外,为了使生活更轻松,您可以缓冲2个元素,将click绑定到它们上并禁用那些未被单击的元素,从而将代码减少为
var btns = $('.up, .down');
btns.click(function(){
btns.not(this).addClass('disable-link');
});
If you'd want to disable the containing a
, the 'other's parent could be obtained by chaining parent() : btns.not(this).parent().addClass('disable-link');
如果你想禁用含有a
“:,对方的父母可以通过链接父()获得btns.not(this).parent().addClass('disable-link');
( or .closest('a')
if there is a deeper hierarchy) (如果层次结构更深, .closest('a')
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.