简体   繁体   English

当我单击其任何一个子元素时,如何获取最顶层的父 ID?

[英]How to get top most parent Id when I click on any one of its child element?

I have the following HTML code:我有以下 HTML 代码:

<div id='parentDiv'>
    <div class='firstDiv'>
         <div class='firstDivChild1'></div>
         <div class='firstDivChild2'>
              <div class='firstDivChild2_Child1'></div>
              <div class='firstDivChild2_Child2'></div>
         </div>
    </div>
    <div class='secondDiv'>
         <div class='secondDivChild1'>
              <div class='secondDivChild1_child'>
                   <div class='secondDivChild1_child_child'></div>
              </div>
         </div>
    </div>
</div>

Now my requirement is when I click on any div I want to get an top most parent Id (ie parentDiv ).现在我的要求是,当我单击任何 div 时,我想获得一个最高父 ID(即parentDiv )。 Presently I'm using the below script to get parent Id:目前我正在使用以下脚本来获取父 ID:

<script type='text/javascript'>
$('div').click(function(e){
   var parentDivId = (e.target).parentNode.id;
   alert(parentDivId );
});
</script>

but it doesn't work.但它不起作用。 Can anyone correct this code to reach my requirement?任何人都可以更正此代码以达到我的要求吗?

If that parent DIV is unique across document, then you just can refer to it by ID, ie $('#parentDiv'), but if it's not, then you should change your HTML and add to parentDiv some class (ie parentDiv), and you'll be able to refer to it by this expression $(this).parents('.parentDiv:first');如果该父 DIV 在文档中是唯一的,那么您可以通过 ID 引用它,即 $('#parentDiv'),但如果不是,那么您应该更改 HTML 并向parentDiv添加一些 class(即 parentDiv),你可以通过这个表达式引用它 $(this).parents('.parentDiv:first');

then use the natural power of event bubbling .然后使用事件冒泡的自然力量 any descendant clicked will bubble up the event upwards (hence bubble) and will act as if the parent is clicked.任何点击的后代都会向上冒泡事件(因此冒泡),并且就像点击父级一样。 so adding a click handler to the parent also does the same thing.所以向父级添加点击处理程序也可以做同样的事情。

$('#parentDiv').on('click',function(){
    var id = this.id
});
$('div').click(function() {
    alert($(this).parents('div').last().attr('id'));
    return false;
});​

Live DEMO现场演示

Try this little function:试试这个小 function:

$.fn.root = function() {
  var $all = $( this[0] ).parents(); 

  // omit "html", "body" and one index to the last item;
  return $all.slice( $all.length - 3, $all.length - 2 );
};

Sample Usage:样品用法:

$('input').click(function() {
    alert($(this).root().prop('id'));
});​

Simple working example using your HTML here在此处使用 HTML 的简单工作示例

It is still not completely obvious what you're asking for, but based on a few of your comments, here's my best guess.您的要求仍然不完全清楚,但根据您的一些评论,这是我最好的猜测。

Using event bubbling, you can examine all clicks in your document and you then determine where the click originated with e.target and you can then figure out whether that click originated in your div tree or elsewhere:使用事件冒泡,您可以检查文档中的所有点击,然后确定点击源自e.target的位置,然后您可以确定该点击是源自您的 div 树还是其他地方:

$(document).click(function(e) {
    // determine if click was in our div tree or not
    if ($(event.target).closest("#parentDiv").length) {
        // click was in our parentDiv tree
    } else {
        // click was not in our parentDiv tree
    }
});

Regardless of where the click was located, you can get the top of your div tree id="parentDiv" at any time with this with this jQuery:无论点击位于何处,您都可以随时使用此 jQuery 获得 div 树id="parentDiv"的顶部:

$("#parentDiv")

If, you just want the top-most div that is above what is clicked, no matter where the click is in the document, you can use event bubbling like this to get that:如果,您只想要点击内容上方的最顶层 div,则无论点击在文档中的哪个位置,您都可以使用这样的事件冒泡来获得:

$(document).click(function(e) {
    e.stopPropagation();
    var topMostDiv = $(e.target).parents("div").last();
    // do whatever you want with topMostDiv here
});

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

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