jQuery获取同级或最接近的元素不起作用
jQuery get sibling or closest element not working
我就是无法让这一小段代码正常工作。我基本上是想得到点击的ahref的兄弟,但它只是返回点击的元素本身(使用console.log()
来测试这一点)。
$('.script-vote').click(function(e) {
e.preventDefault();
var href = $(this);
href.siblings('.script-vote') //This doesn't work
.removeClass('active');
href.closest('.script-vote') //This doesn't work
.removeClass('active');
});
还有我的HTML:
<div class="one-half t-center">
<a href="..." class="script-vote up" title="Upvoten">
<i class="fa fa-thumbs-o-up"></i>
<span class="score"><?=$post->getUpVotes()?></span>
</a>
</div>
<div class="one-half t-center">
<a href="..." class="script-vote down" title="Downvoten">
<i class="fa fa-thumbs-o-down"></i>
<span class="score"><?=$post->getUpVotes()?></span>
</a>
</div>
因此,当我单击.script-vote.up
元素时,我想删除.script-vote.down
的active
类。提前谢谢。
;a> HTML中的元素没有任何同级元素。但他们的父母会这样做。确保他们被封装在一个grand-parentdiv:中
<div class="voting-container">
<div class="one-half t-center">
<a href="..." class="script-vote up" title="Upvoten">
<i class="fa fa-thumbs-o-up"></i>
<span class="score"><?=$post->getUpVotes()?></span>
</a>
</div>
<div class="one-half t-center">
<a href="..." class="script-vote down" title="Downvoten">
<i class="fa fa-thumbs-o-down"></i>
<span class="score"><?=$post->getUpVotes()?></span>
</a>
</div>
</div>
您可以使用jQuery参考以下JavaScript作为示例:
$(".script-vote").click(function(){
var otherVotingButton = $(this).parent().siblings(".one-half").first().find(".script-vote");
otherVotingButton.css("background-color","red");
otherVotingButton.href="#";
otherVotingButton
.off() //remove original event handler
.click(function() { alert("you already voted!");});
});
获取父级的兄弟姐妹,然后获取脚本投票<a> 元素。
相关文章:
- iframe中的jQuery查找元素不起作用
- jQuery选择ajax调用中附加的元素不起作用
- 我的ID获取元素不起作用
- 为什么从 Angular 服务中的对象中删除此元素不起作用
- 将onclick事件附加到元素不起作用
- <标题>插入SVG的元素不起作用
- 使用next()的jQuery目标元素不起作用
- HTML输入元素不起作用
- 使用css显示和隐藏元素不起作用
- 焦点到 DOM 元素不起作用
- Jquery 在图像错误上对多个元素不起作用
- 按类名获取元素不起作用
- jQuery - 'addClass' to HTML 元素 - 不起作用,但在 Chrome 中正确显
- 具有 ID AND 类的 jQuery 选择器目标元素不起作用
- Ajax 对 xml 页面的响应以获取标题元素不起作用
- 单击新 DOM 元素不起作用的事件
- Javascript 添加的动态 svg 元素不起作用
- jQuery on handler 对插入的元素不起作用
- 从表单中删除添加的元素不起作用
- 相对于另一个元素的位置元素不起作用