最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get top most parent Id when I click on any one of its child element? - Stack Overflow

programmeradmin7浏览0评论

I have the following HTML code:

<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 (i.e. parentDiv). Presently I'm using the below script to get parent 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?

I have the following HTML code:

<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 (i.e. parentDiv). Presently I'm using the below script to get parent 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?

Share Improve this question edited Aug 9, 2022 at 10:47 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 19, 2012 at 5:07 Valli69Valli69 9,9226 gold badges26 silver badges29 bronze badges 10
  • 1 can u define clearly level of topmost? – Govind Malviya Commented Apr 19, 2012 at 5:13
  • @GovindKamalaPrakashMalviya I need the id of first div, i.e. <div id='parentDiv'> id – Valli69 Commented Apr 19, 2012 at 5:23
  • <html> elements don't have id's :P – botbot Commented Apr 19, 2012 at 5:35
  • It is still unclear what you want. If someone clicks on firstDivChild2_Child1, what id do you want? – jfriend00 Commented Apr 19, 2012 at 5:51
  • 3 @Pravallika69 - you've asked 9 questions and NEVER accept an answer. That is not how SO works. Go back to your previous questions and pick the best answer and accept that by clicking on the checkmark to the left of the answer. Otherwise, some people will find little point in trying to help you because you aren't rewarding anyone for helping you. You should also upvote all answers that provided some help. – jfriend00 Commented Apr 19, 2012 at 11:01
 |  Show 5 more ments

5 Answers 5

Reset to default 2

If that parent DIV is unique across document, then you just can refer to it by ID, i.e. $('#parentDiv'), but if it's not, then you should change your HTML and add to parentDiv some class (i.e. parentDiv), and you'll be able to refer to it by this expression $(this).parents('.parentDiv:first');

$('div').click(function() {
    alert($(this).parents('div').last().attr('id'));
    return false;
});​

Live DEMO

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
});

It is still not pletely obvious what you're asking for, but based on a few of your ments, 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:

$(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:

$("#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:

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

Try this little 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

发布评论

评论列表(0)

  1. 暂无评论