警告对话框的结果显示为空。我无法访问父节点的父节点
var bt = document.getElementById("btn1");
bt.onclick = function() {
alert(bt.parentNode.parentNode.className);
}
<html>
<head></head>
<body>
<div class="container">
<div class="template">
<div class="img "> <img src="images/demo/hotel1.jpg"> </div>
<div class="hname animated slideInRight">
<h2>Golden Tulip <span id="city">Lucknow</span></h2>
</div>
<div class="address animated slideInLeft animated slideInLeft"> <span id="city"><b>Lucknow,<b></span> 2km to city centre. <button id="btn1">View Deal</button> </div>
<div class="rating"> 5 star </div>
</div>
</div>
</body>
</html>
答案 0 :(得分:2)
您错过了关闭b
标记:<b>Lucknow,<b>
这可能导致浏览器通过关闭该标记来纠正错误的结构。结果,额外的b
元素作为父级添加到按钮的层次结构中。得到的结构是:
<b>
<b>
2km to city centre.
<button id="btn1">View Deal</button>
</b>
</b>
当您关闭b
标记时,结果很好:
var bt = document.getElementById("btn1");
bt.onclick = function() {
alert(bt.parentNode.parentNode.className);
}
&#13;
<div class="container">
<div class="template">
<div class="img "> <img src="images/demo/hotel1.jpg"> </div>
<div class="hname animated slideInRight">
<h2>Golden Tulip <span id="city">Lucknow</span></h2>
</div>
<div class="address animated slideInLeft animated slideInLeft"> <span id="city"><b>Lucknow,</b></span> 2km to city centre. <button id="btn1">View Deal</button> </div>
<div class="rating"> 5 star </div>
</div>
</div>
<!--THE RESULT OF ALERT DIALOG COMES EMPTY.I AM NOT ABLE TO ACCESS THE PARENT OF PARENT NODE-->
&#13;