如何仅针对 li 项目而不是其中的单选按钮

Posted

技术标签:

【中文标题】如何仅针对 li 项目而不是其中的单选按钮【英文标题】:How to target only li item and not radio button inside it 【发布时间】:2017-02-21 06:13:40 【问题描述】:

我在 UL 中有清单项目。我想点击一个特定的列表项并执行点击,但是 li 里面有一个单选按钮,当我点击它时,点击甚至是触发。我如何针对 li 项目而不是其中的收音机。我尝试了 :not 选择器,它确实成功了。任何帮助,将不胜感激。 谢谢。

<li>
    <h5><a class="tooltiptext" href="show">Click</a></h5>
    <label for="itemA">
        <img src="images/templates/template1.png"></img>
    </label><br />
    <input type="radio" id="itemA" class="chooseItem" name="chooseTemplate" value="itemA" checked>
    Select
</li>
$("li:nth-child(1):not(input[type='radio']").click(function(e)
)

它可以工作,但是当我点击收音机时,我不想触发点击事件。

【问题讨论】:

【参考方案1】:

你可以像下面那样做。

 $("li:nth-child(1)").click(function(e)
     if(!$(e.target).is(':radio')) 
         //you code goes here
     
 )

【讨论】:

【参考方案2】:

检查事件的目标不是收音机或它的关联标签,因为点击标签会改变收音机状态

$("li:nth-child(1)").click(function(e)
   if(!$(e.target).is('label[for], :radio') )
      // not a radio click heree
   
)

【讨论】:

以上是关于如何仅针对 li 项目而不是其中的单选按钮的主要内容,如果未能解决你的问题,请参考以下文章

为啥使用 StringVar 而不是 IntVar 时 Tkinter 的单选按钮都开始选中?

如何使我的单选按钮成为必需的[重复]

基于选定的单选按钮选项,在闪亮的R中单击提交按钮后如何显示图表和数据?

将单选按钮关联到各个组

使用图像而不是单选按钮

更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本