dpca31461 2016-11-22 07:21
浏览 5

jQuery一个接一个地影响元素,而不是影响被点击的元素

I'm echoing out a number of div tags dynamicaly like:

foreach($applications as $application){
    $id =  $application->user_id;
    echo '<div class="col-md-4" id="'.$id.'">',
              '<h4 class="list-group-item-heading remove-margin">
                    <i class="fa fa-plus fa-fw" id="'.$id.'"> </i>
               </h4>',
         '</div>';
}

i want to make it so that when a user clicks the plus button in a selected div, then only that div is affected. So far when I do that, it affects the elements in sequential order, nomatter which plus I click. here's the jQuery:

 $(document).ready(function(){
            $('.fa-plus').click(function(){
                    counter = 0;
                    id      = $(this).closest('div').prop("id");
                    counter = counter+1;
                    $(this).css('color','green');
                    $('#votes-count').parent().html(counter);
                    alert(id);                  
            });

    });
  • 写回答

4条回答 默认 最新

  • dourui7186 2016-11-22 07:51
    关注

    You can use the event object from the click event to get the id of the clicked element.

    $(document).ready(function(){
            $('.fa-plus').click(function(evt){
                    counter = 0;
                    id      = evt.target.id;
    
                    if($(this).attr("id") != id){
                        return;
                    }
    
                    counter = counter+1;
                    $("#" + id).css('color','green');
                    $('#votes-count').parent().html(counter);
                    alert(id);                  
            });
    });
    

    You can refer here for more info about the event object that is passed.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入