[英]How to select parent of element than of that parent child with id/class on click?
I trying to make comments section that will show on button click, but every comments section on every post have same id
/ class
.我试图制作将在单击按钮时显示的评论部分,但每个帖子的每个评论部分都有相同的
id
/ class
。 And I try to select parent of element that is clicked than of that parent child with id
/ class
.我尝试 select 元素的父元素被点击,而不是
id
/ class
的父子元素。
function ShowComments(str, item) { if (item.innerHTML == "") { console.log("Show"); var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { item.innerHTML = this.responseText; } }; xmlhttp.open("GET", "getcomments.php?q=" + str, true); xmlhttp.send(); } else { item.innerHTML = ""; } }
<div> <button style="font-size: 20px;float: right;" onclick="ShowComments('.$row['id'].', this > div.comments)">Comments</button><br> <div class="comments" style="font-size: 20px; float: left; border-style: solid; border-width: 0.5px;"></div> </div>
You can achive it by pasting this
to the function, that way you can target the parent node of the button by using .parentNode
您可以通过将其粘贴
this
function 来实现它,这样您就可以使用.parentNode
按钮的父节点
function myfunction(e){ console.log(e.parentNode.classList.value) }
<div class="myclass"> <button onclick="myfunction(this)">button</button> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.