简体   繁体   English

jQuery选择器可否定父元素中的特定HTML元素

[英]JQuery selector to negate specific HTML elements within a parent element

I have HTML label structures generated by a JQuery multiselect dropdown library as shown: 我有一个由JQuery multiselect下拉库生成的HTML标签结构,如下所示:

<label for="ui-multiselect-selectedSku-option-1"><input id="ui-multiselect-dropdown-option-1" type="radio" value="DropDownVal1"><span>DropDownText1</span></label>
<label for="ui-multiselect-selectedSku-option-2"><input id="ui-multiselect-dropdown-option-2" type="radio" value="DropDownVal2"><span>DropDownText2</span></label>

My requirement is: except the input element, whenever the user clicks on anywhere else in the 我的要求是:除了input元素外,只要用户在

<label></label>

(including the label) area,I need to do event.preventDefault() . (包括标签)区域,我需要执行event.preventDefault() I have tried as 我尝试过

 $(document).on('click', 'label[for^="ui-multiselect-selectedSku-option-"]',function(event){
    event.preventDefault();
});

But the above handler gets triggered even when I click on the <input> within the label as well(which is obvious and I know that!) 但是即使我也单击标签内的<input> ,上述处理程序也会被触发(这很明显,我知道!)

How do I write a JQuery selector for to filter this. 如何编写用于过滤此内容的JQuery选择器。

The best thing would be to have the input out of the label... But: 最好的办法是将输入内容从标签中删除...但是:

I do not have control over this markup structure... (your comment on another answer) 我无法控制此标记结构...(您对另一个答案的评论)

You can use .stopImmediatePropagation() on the <input> elements... So the click event won't bubble up to the label. 您可以在<input>元素上使用.stopImmediatePropagation() 。因此,click事件不会冒泡到标签上。

See below... Try a click a label, then on a radio. 参见下文...尝试单击标签,然后在收音机上。

 $(document).on('click', 'label[for^="ui-multiselect-selectedSku-option-"]',function(event){ console.log("Clicked on a label"); event.preventDefault(); // Don't know if that is useful... }); $(document).on('click', 'label[for^="ui-multiselect-selectedSku-option-"] input',function(event){ console.log("Clicked on a radio input"); event.stopImmediatePropagation(); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label for="ui-multiselect-selectedSku-option-1"><input id="ui-multiselect-dropdown-option-1" type="radio" value="DropDownVal1"><span>DropDownText1</span></label> <label for="ui-multiselect-selectedSku-option-2"><input id="ui-multiselect-dropdown-option-2" type="radio" value="DropDownVal2"><span>DropDownText2</span></label> 

The first problem is your label. 第一个问题是您的标签。 You generally should either have your input inside the label OR use the for attribute. 通常,您应该将输入内容包含在标签内,或者使用for属性。 Your for attributes are for different controls than the ones inside the labels. 您的for属性用于与标签内的控件不同的控件。 I am not sure how browsers will handle this. 我不确定浏览器将如何处理。

If you don't want clicks on the label to trigger an input, don't associate them at all. 如果您不想单击标签来触发输入,请不要将它们关联起来。 Consider not using a label at all, use a span instead. 考虑根本不使用标签,而应使用跨度。

If there is a condition that turns on/off whether the label affects a control, you can use code to set/clear the label's for attribute. 如果存在打开/关闭标签是否影响控件的条件,则可以使用代码设置/清除标签的for属性。

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

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