English 中文(简体)
Javascript: 微薄元素,但不包括儿童
原标题:Javascript: Clicking a wrapper element but not a child element

我撰写了一些 j(jQuery),在被点击时能够围绕检查箱进行包装。 然而,Im 的问题在于,当你在检查箱上点击时,它却不工作,因为它是两倍的(至少我认为,我们会知道所发生的事情)。

Here s a demo.

Here s the code:

$( .checkbox-wrapper ).click(function(){
    var $checkbox = $(this).find( input[type="checkbox"] );
    if ($checkbox.is( :checked )) {
        $checkbox.attr( checked , false);
    } else {
        $checkbox.attr( checked , true);
    }
});

我怎么能够做到,把检查箱点点击成正常工作,但如果你在周围地区点击,则把检查箱打到一起?

Solution:

感谢 j们评论了如何通过检查活动来做到这一点。 目标财产:

$( .checkbox-wrapper ).click(function(e){
    if( e.target.nodeName ===  INPUT  ) {
        e.stopPropagation();
        return;
    }
    
    var $checkbox = $(this).find( input[type="checkbox"] );
    if ($checkbox.is( :checked )) {
        $checkbox.attr( checked , false);
    } else {
        $checkbox.attr( checked , true);
    }
});

正如其他人指出的,这也许不是最好的例子,因为你具有同样的功能(不需要javascript),把检查箱装上标签标签,而不是贴上标签。

最佳回答

Check the event.target property.

The target property can be the element that registered for the event or a descendant of it. It is often useful to compare event.target to this in order to determine if the event is being handled due to event bubbling.

问题回答

引言

$( .checkbox-wrapper ).click(function(e){
   if(!$(e.target).is(":checkbox")){
     var $checkbox = $(this).find( input[type="checkbox"] );
     if ($checkbox.is( :checked )) {
         $checkbox.attr( checked , false);
     } else {
         $checkbox.attr( checked , true);
     }
   }
});

仅包装一个<代码><label>周围。

象你这样说,你不会停止把事情传给家长。

• 如何阻止在检查箱上铺设会议





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签