在`span`标签上捕获onClick事件

时间:2021-06-02 23:59:52

I have got a lot of span tags inside my div elements. Each div has a unique id.I want to add an onclick event to the span elements in each div uniquley. How can i do that?

我的div元素中有很多span标签。每个div都有一个唯一的id。我想在每个div uniquley中的span元素中添加一个onclick事件。我怎样才能做到这一点?

  <div class="bootstrap-tagsinput"id="1">
  <span class="tag label label-info">Access control lists</span> 
  <span class="tag label label-info">Network firewall</span> 
  </div>


  <div class="bootstrap-tagsinput"id="2">
  <span class="tag label label-info">Access control lists</span> 
  <span class="tag label label-info">Network firewall</span> 
  </div>

My current script which catches all the span elements irrespective of the div.

我当前的脚本捕获所有span元素而不管div。

      $('span.label-info').click(function() {

         var news=$('input[type=text][id="vuln<?php echo $model->v_id;?>"]').val()+','+$(this).text();
         $('input[type=text][id="vuln<?php echo $model->v_id;?>"]').val(news);
        return false;
});

How can i catch click on span elements inside each div separately?

我怎样才能分别点击每个div内的span元素?

3 个解决方案

#1


1  

I think you're looking at it the wrong way. Instead of having a separate click handler for each div, you can keep the shared click handler, but find out which div is the parent, and use its ID in the subsequent selectors.

我觉得你看错了。您可以保留共享点击处理程序,但找出哪个div是父级,并在后续选择器中使用其ID,而不是为每个div设置单独的单击处理程序。

$('span.label-info').click(function() {

    // find the parent/ansector that is a div and has the class
    var divId = $(this).closest('div.bootstrap-tagsinput').prop('id');

    var news=$('input[type=text][id="vuln' + divId + '"]').val()+','+$(this).text();
    $('input[type=text][id="vuln' + divId + '"]').val(news);
    return false;
});

#2


0  

Your code should work to add a click even handler to each span.

您的代码应该可以为每个范围添加一个单击甚至处理程序。

$('span.label-info').click(function() {
    $(this).attr('id'); //<--$(this) is a handle to the clicked span
    $(this).parent().attr('id'); //<-- $(this).parent() references the parent div
});

It's worth mentioning, consider changing .click() to .on('click').

值得一提的是,考虑将.click()更改为.on('click')。

Also - if you wanted to bind once without rebinding after spans were dynamically added to the div, you could bind it like this:

另外 - 如果你想在spans动态添加到div之后没有重新绑定就绑定一次,你可以像这样绑定它:

$('div').on('click', 'span.label-info').click(function() {  
   /* <-- do stuff with $(this) or $(this).parent() here --> */
});

#3


0  

http://jsfiddle.net/GZVdL/3/

Hi.

in your span click event you can find parent div and then get its attribute id to find which div it is. Below is the code

在您的span单击事件中,您可以找到父div,然后获取其属性ID以查找它是哪个div。以下是代码

$(document).ready(function(){


$(".tag").click(function(){


    alert($(this).parent().attr("id"));
});

}); 

#1


1  

I think you're looking at it the wrong way. Instead of having a separate click handler for each div, you can keep the shared click handler, but find out which div is the parent, and use its ID in the subsequent selectors.

我觉得你看错了。您可以保留共享点击处理程序,但找出哪个div是父级,并在后续选择器中使用其ID,而不是为每个div设置单独的单击处理程序。

$('span.label-info').click(function() {

    // find the parent/ansector that is a div and has the class
    var divId = $(this).closest('div.bootstrap-tagsinput').prop('id');

    var news=$('input[type=text][id="vuln' + divId + '"]').val()+','+$(this).text();
    $('input[type=text][id="vuln' + divId + '"]').val(news);
    return false;
});

#2


0  

Your code should work to add a click even handler to each span.

您的代码应该可以为每个范围添加一个单击甚至处理程序。

$('span.label-info').click(function() {
    $(this).attr('id'); //<--$(this) is a handle to the clicked span
    $(this).parent().attr('id'); //<-- $(this).parent() references the parent div
});

It's worth mentioning, consider changing .click() to .on('click').

值得一提的是,考虑将.click()更改为.on('click')。

Also - if you wanted to bind once without rebinding after spans were dynamically added to the div, you could bind it like this:

另外 - 如果你想在spans动态添加到div之后没有重新绑定就绑定一次,你可以像这样绑定它:

$('div').on('click', 'span.label-info').click(function() {  
   /* <-- do stuff with $(this) or $(this).parent() here --> */
});

#3


0  

http://jsfiddle.net/GZVdL/3/

Hi.

in your span click event you can find parent div and then get its attribute id to find which div it is. Below is the code

在您的span单击事件中,您可以找到父div,然后获取其属性ID以查找它是哪个div。以下是代码

$(document).ready(function(){


$(".tag").click(function(){


    alert($(this).parent().attr("id"));
});

});