如何获取在父标记之前出现的textContent?

时间:2023-02-13 15:07:09

I am trying to get the textContent of a h3 tag that appears above my parent div tag. The code is repeated, and adding an id is not an option for h3 tag. looking to do this in pure javascript. My html currently is...

我正在尝试获取出现在父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>  

this piece of code repeats many times, and the textContent of h3 and the div id changes everytime. I am trying to return the value of the textContent of the h3 tag that appears about the span button i am clicking relative to the div tag that appears during the click. The h3 tag is not part of any other parent/child relationships, and is stand along...

这段代码重复多次,h3的textContent和div id每次都会改变。我试图返回h3标签的textContent的值,该标签出现在我点击相对于点击期间出现的div标签的span按钮上。 h3标签不是任何其他父/子关系的一部分,并且是站在...

3 个解决方案

#1


0  

document.getElementByID("someid").parentElement.previousElementSibling.textContent;

的document.getElementById( “someid”)parentElement.previousElementSibling.textContent。

Edited

编辑

#2


1  

From the click handler of the span you can access the above h3 element like this:

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

this.parentNode.previousSibling.textContent

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

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

#3


0  

let content = document.querySelectorAll('h3');

let count = content.length;

if (count) {

  for (let i = 0; i < count; i++) {
    alert(content[i].textContent);      
  }
}

https://jsfiddle.net/2kk49yLp/1/

https://jsfiddle.net/2kk49yLp/1/

#1


0  

document.getElementByID("someid").parentElement.previousElementSibling.textContent;

的document.getElementById( “someid”)parentElement.previousElementSibling.textContent。

Edited

编辑

#2


1  

From the click handler of the span you can access the above h3 element like this:

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

this.parentNode.previousSibling.textContent

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

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

#3


0  

let content = document.querySelectorAll('h3');

let count = content.length;

if (count) {

  for (let i = 0; i < count; i++) {
    alert(content[i].textContent);      
  }
}

https://jsfiddle.net/2kk49yLp/1/

https://jsfiddle.net/2kk49yLp/1/