简体   繁体   English

当我点击父母中的一个孩子时,如何防止jquery选择父母?

[英]How can I prevent jquery from selecting a parent when I click on one of the children in that parent?

ok quick scenario: 好快速方案:

html: HTML:

<span class="answer">blah<input type="radio" value="1"></span>

jquery: jQuery的:

$("span.answer").click(
function() {
check = $("input", this);
check.attr('checked', check.attr('checked') === true ? false : true);
);

Ok so this will check/uncheck the child radio input inside the selected span when I click inside it. 好的,这样当我点击它内部时,这将检查/取消选中所选范围内的子无线电输入。

The problem I have is that I don't want to run this event when I actually click on the radio button itself (obviously because jquery will see the radio button as checked and uncheck - in effect the exact opposite of what should happen usually). 我遇到的问题是,当我实际点击单选按钮本身时,我不想运行此事件(显然因为jquery会将单选按钮视为已选中并取消选中 - 实际上与通常应该发生的情况完全相反)。 Something along the lines of this: 有点像这样:

$("span.answer:not(span input)").click

This of course doesn't work. 这当然不起作用。 Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

Leo 狮子座

$("span.answer input").click(function(evt)
{
    evt.stopPropagation();
});

Will prevent the click event from bubbling up (and triggering the handler on "span.answer".) 将阻止click事件冒泡(并在“span.answer”上触发处理程序。)

There is a specific html tag to accomplish what you try to do with jquery and javascript .. 有一个特定的html标记来完成你尝试用jquery和javascript做的事情。
it is the label tag 它是label标签

<label>blah<input type="radio" value="1" /></label>

this will have the effect you want 这将产生你想要的效果

[update] For Internet Explorer 6 to play nice use the complete syntax of the label by using the for attribute which targets the id of an form input/select/etc.. element.. [更新]为了让Internet Explorer 6更好地使用标签的完整语法,使用for属性,该属性以表单输入/ select / etc ..元素的id为目标。

<label for="radio_1">blah<input id="radio_1" type="radio" value="1" /></label>

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

相关问题 我有很多班级新闻的家长元素,我该如何从同一个父母的其他孩子中选择一个孩子 - I've many parent elements with class news how can i select children on one from other children in same parent 单击父级时如何获取儿童 ID? - How to get children id when I click the parent? 如何防止父元素事件从子事件中的代码执行 - How can I prevent parent element event execution from code inside children event php jstree仅当我单击父级以展开时如何从数据库加载子级 - php jstree how to load children from database only when i click the parent to expand 如何防止孩子点击事件冒泡给父母? - How can I prevent child click event bubbling to parent? 如何自动select儿童选项选择时选择父级选项JS,ZA565B176ECE81D91B5C08C08C39ADB65AF1Z - How can i auto select children option when selected parent option Js, Jquery 如何从 React 中的父级访问子组件中的变量? - How can I access variables in children components from parent in React? 从子级拖动到父级时如何防止父级单击事件 - How to prevent parent click event when dragging from child to parent 当我使用jQuery单击一个div时如何获取相对于父div的offsetX - how to get offsetX that Relative to the parent div when i click one div using jquery jQuery - 单击父级继承自子级 - jQuery - click on parent is inherited from children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM