admin管理员组

文章数量:1402836

I have the following code:

<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8"/>
<script type = "text/javascript" src = "js/jquery.js" ></script>
</head>
<body>
<div id = 'div'>
    <div id = "1">

    </div>     
    <div id = "2">

    </div>  
</div>         
</body>
</html>

My JavaScript code is:

$(document).ready(function(){
    var lastmentq   = document.getElementById('div').lastChild.id;
    alert(lastmentq);
});

It should alert the id of the lastchild of the div with the id 'div' which is '2' but I am getting the alert as "undefined". I don't know what I have done wrong. Please help me.

I have the following code:

<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8"/>
<script type = "text/javascript" src = "js/jquery.js" ></script>
</head>
<body>
<div id = 'div'>
    <div id = "1">

    </div>     
    <div id = "2">

    </div>  
</div>         
</body>
</html>

My JavaScript code is:

$(document).ready(function(){
    var lastmentq   = document.getElementById('div').lastChild.id;
    alert(lastmentq);
});

It should alert the id of the lastchild of the div with the id 'div' which is '2' but I am getting the alert as "undefined". I don't know what I have done wrong. Please help me.

Share Improve this question edited Aug 20, 2013 at 18:08 user1763032 asked Aug 20, 2013 at 17:57 user1763032user1763032 4271 gold badge9 silver badges24 bronze badges 5
  • You are alerting lastmentq, what's that? Your variable is called lastchild. – gen_Eric Commented Aug 20, 2013 at 18:00
  • 3 if you are using jquery, why are you using getElementById instead of jQuery's native selectors? – Neil S Commented Aug 20, 2013 at 18:00
  • @RocketHazmat That was a mistake . i corrected it.. – user1763032 Commented Aug 20, 2013 at 18:01
  • 2 Putting your <script> pletely outside the <html> probably works, but it offends even my anti-formalist sensibilities. – Pointy Commented Aug 20, 2013 at 18:03
  • Thanks to all who have answered and mented..thank you very much.. – user1763032 Commented Aug 20, 2013 at 18:16
Add a ment  | 

6 Answers 6

Reset to default 8

Your elements probably have text nodes around them, so the last child node of the outer <div> won't necessarily have an "id" attribute.

I'm not sure if all browsers support it, but there's a "lastElementChild" property that explicitly gets only elements, and not things like ment nodes or text nodes. Failing that, you could just loop through the node list looking for type 1 nodes.

is this your wanted behaivour?

$(document).ready(function(){
     var lastchild   = $("div").last().attr("id")
     alert(lastchild);
     });


<div id="div">
    <div id ="1">

    </div>     
    <div id="2">

    </div>  
</div>        

check out this fiddle for live example

http://jsfiddle/sHgbF/

In jquery:

$(function(){
  alert($("#div :last-child").attr('id'));
});

The jQuery way:

// assuming the last child is always a div
var lastmentq = $('#div > div:last-child').attr('id');

// alternatively
var lastmentq0 = $('#div').children('div').last().attr('id');

The JavaScript way:

var lastmentq = document.getElementById('div').lastElementChild.id;

Note that this works in all modern browsers and IE 9+. See lastElementChild on MDN.

this is what I would have done, but I'm not clear as to if it's what you want:

$(function () {
    var lastchild = $('#div div:last-child').attr('id');
    alert(lastchild);
});

http://jsfiddle/pFjPS/

also, I don't believe classes or ids can start with numbers, so your markup is probably not valid

edit : HTML5 supports it, but is not generally remended.

I would use this approach, since ID is a property and not an attribute.

$(function () {
    var lastchild = $('#div div:last-child').prop('id');
    alert(lastchild);
});

本文标签: javascriptlastChild not workingStack Overflow