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 calledlastchild
. – 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
6 Answers
Reset to default 8Your 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
版权声明:本文标题:javascript - lastChild not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744373208a2603132.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论