gpt4 book ai didi

javascript - 如何获取出现在父标签之前的 textContent?

转载 作者:行者123 更新时间:2023-11-28 00:55:21 25 4
gpt4 key购买 nike

我正在尝试获取出现在父 div 标签上方的 h3 标签的 textContent。代码重复,添加 id 不是 h3 标签的选项。希望在纯 Javascript 中做到这一点。我的 html 目前是...

<h3>Some header</h3> //textContent I am trying to get.
<div class="panel panel-success"> //parent
<div id="someid" class="panel-heading"></div> //child
<span class="pull-right glyphicon glyphicon-list-alt" style="padding-right: 1%"
onclick="myFunc(this.parentNode.id, what.goes.here?)"></span> //this needs to return parent id and the value of h3 tag
</div>
<h3>Some other header</h3> //textContent I am trying to get.
<div class="panel panel-success"> //parent
<div id="someid" class="panel-heading"></div> //child
<span class="pull-right glyphicon glyphicon-list-alt" style="padding-right: 1%"
onclick="myFunc(this.parentNode.id, what.goes.here?)"></span> //this needs to return parent id and some other tag when this span button is clicked
</div>
<h3>yet another header</h3> //textContent I am trying to get.
<div class="panel panel-success"> //parent
<div id="someid" class="panel-heading"></div> //child
<span class="pull-right glyphicon glyphicon-list-alt" style="padding-right: 1%"
onclick="myFunc(this.parentNode.id, what.goes.here?)"></span> //this needs to return parent id and yet another header when clicked.
</div>

这段代码重复了很多次,每次h3的textContent和div id都变了。我正在尝试返回关于我正在单击的跨度按钮的 h3 标记的 textContent 的值,相对于单击期间出现的 div 标记。 h3 标签不是任何其他父/子关系的一部分,并且是独立的...

最佳答案

spanclick 处理程序中,您可以像这样访问上面的 h3 元素:

this.parentNode.previousSibling.textContent

参见:https://developer.mozilla.org/en/docs/Web/API/Node/previousSibling

关于javascript - 如何获取出现在父标签之前的 textContent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45224811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com