我有以下代碼:lastChild不工作
<!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>
我的JavaScript代碼是:
$(document).ready(function(){
var lastcommentq = document.getElementById('div').lastChild.id;
alert(lastcommentq);
});
應該警惕與ID爲 '格',這是div的lastchild的id '2',但我得到的警報爲「未定義」。我不知道我做錯了什麼。請幫幫我。
您正在提醒'lastcommentq',那是什麼?你的變量被稱爲'lastchild'。 –
如果您使用jQuery,爲什麼使用getElementById而不是jQuery的本機選擇器? –
@RocketHazmat這是一個錯誤。我糾正了它.. – user1763032