如何获取兄弟div的html值

时间:2014-02-19 19:13:16

标签: php jquery increment decrement

我很抱歉这个标题,但我不知道如何为这个问题设定标题

问题:

我有一个容器div和两个子div。第一个子div包含一个数值,我想在每次点击第二个子div时递增

CODE:

 <div class="container1">
    <span class="like_counter">2</like>
    <span class="like_unlike">Like</span><!--if the text within is like then on click it will change the inner text to unlike and increment the value in like counter by 1.if it is unlike it will decrement it by 1 and change inner text to Like-->
 </div>

请注意,第一个子选择器不是一个选项,因为它会影响每个第一个子div

3 个答案:

答案 0 :(得分:3)

答案,使用prev函数效果不是很好,如果项目顺序发生变化,代码就会停止工作。

试试这个:

$(".like_unlike").bind("click", function(e) {
    e.preventDefault();
    var likeCounter = $(this).siblings(".like_counter");
    likeCounter.html(parseInt(likeCounter.html()) + 1);
});

答案 1 :(得分:1)

您可以使用

$(this).prev()

此处列出了一些更有趣的兄弟搜索以及其他搜索http://home.techphernalia.com/70480/HTML/Selector.html

答案 2 :(得分:0)

这叫做兄弟姐妹。你可以使用

$(this).prev();

https://api.jquery.com/prev/

相关问题