简体   繁体   English

Bootstrap列表项在嵌入式跨度上激活

[英]Bootstrap list-items activating on embedded span

Sorry for the title, I found no better way to describe it. 抱歉,标题找不到更好的描述方式。

I have a list-group and want the buttons to show a specific color when active. 我有一个列表组,希望按钮在活动时显示特定的颜色。 but somehow, the embedded spans capture the click and seem to not count as part of the a . 但不知何故,嵌入式跨度捕获点击,似乎不能算作该部分a

how can I fix this? 我怎样才能解决这个问题? I want the button to change color, no matter where I click (on span or anywhere else) 无论我在哪里单击(跨度或其他任何位置),我都希望按钮更改颜色

The code is here: https://jsfiddle.net/zj6uwmvu/ 代码在这里: https : //jsfiddle.net/zj6uwmvu/

thanks 谢谢

instead of getting the item by "a", try getting it by its class like this: 而不是通过“ a”获取项目,请尝试按如下所示的类别进行获取:

.list-group-item.active, .list-group-item.active:focus, .list-group-item.active:hover{
          background-color: red; //instead of red put the color that you want.
}

Here is the revised code for your click handler. 这是您的点击处理程序的修改后的代码。 If the event target is not a link, it means that the child badge was clicked. 如果事件目标不是链接,则表示已单击子徽章。 If this is the case, we find the closest link (the parent) and assign it as the target. 在这种情况下,我们找到最接近的链接(父链接)并将其分配为目标。

$('.location').find('.location-picker-list .list-group-item').on('click', function(e) {
    var target = $(event.target);
    if (!target.is('a')) {
      target = target.parent('a')
    } 

  e.preventDefault()
  target.closest('.list-group').children(".active").removeClass('active')
  target.addClass('active')
})

在此处输入图片说明

https://jsfiddle.net/zj6uwmvu/11/ https://jsfiddle.net/zj6uwmvu/11/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM