简体   繁体   English

如何从jQuery页面上的点击事件检查父div

[英]how to check the parent div, from click event on the page from jquery

I want to check if I am clicking inside the div with classname as 'myclass' (in any child inside the div ). 我想检查是否在div内单击类名为'myclass'的div(在div内的任何子级中)。 this code is returns only the current control in which we click , I want to check its all parents , if it has div with classname 'myclass' 此代码仅返回我们单击的当前控件,如果它的类名为“ myclass”,我想检查其所有父级

$("body").click(function (e) {
    if (e.target.className !== "myclass") {
        doo();
    }
    alert(e.target.id);
    alert(e.target.className);
});
$("body").click(function (e) {
   var elem = $('.myClass'); 
   if ((!elem.is(e.target)) && (elem.has(e.target).length === 0)) { 
       doo(); 
   }
});

Try this SEE DEMO 试试这个看演示

$('div').on('click',function(){
    var $parent = $(this).parent();
    if( $parent.hasClass('myClass') ){
        alert( $(this).text() );
    }
});

you can use closest to find the parent which is having myclass 您可以使用最接近的找到具有myclass的父级

try this 尝试这个

$("body").click(function (e) {
    if($(e.target).closest('.myclass').length == 0 && $(e.target).attr('class') != "myclass"){
        doo();
    }
});

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

相关问题 jQuery单击事件从父页面元素 - JQuery click event for element from parent page jQuery click事件:检查元素是否在父div内 - jQuery click event: check if element is within parent div 从父 div 的子级禁用单击事件 - Disabling a click event from the child of a parent div 如果所有div具有相同的类,如何在jquery click事件中从多个div中选择一个div? - how to select a div from multiple div on jquery click event if all div have same class? 如何停止切换jQuery中复选框单击事件的div可见性 - how to stop toggling visibility of div on check box click event in Jquery 在iframe中的图像上绑定来自父页面的点击事件 - bind click event from parent page on image within Iframe 从父页面触发的点击事件在IE中不起作用 - Click event triggered from parent page not working in IE 如何展示 <div> 使用JS,jQuery从2.html(其中按钮在另一个HTML页面上可用)在按钮单击事件上的1.html页面上可用 - how to show <div> available on 1.html page on button click event from 2.html(where button is available on another HTML page ) using JS, jquery 从子级拖动到父级时如何防止父级单击事件 - How to prevent parent click event when dragging from child to parent 如何将Java脚本添加到“从用户控制按钮单击事件到父页面” - How to add java script to the from user control button click event to parent page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM