英文:
How can I get h1 innerText in JavaScript without the innerText of its child?
问题
I want to get the innerText of <h1>
without the innerText
inside the span... this is the HTML of the page:
var title = document.querySelector('div.col-md-8.info-header h1');
title = title && title.innerText;
console.log(title);
<div class="col-md-12 header">
<div class="col-md-8 info-header">
<h1> This note is for h1 tag!!!!!! <span> this note is insidespan v226hql!!! </span>
</h1>
</div>
</div>
but this will return the innerText
of both <h1>
and <span>
.
what can I do?
英文:
I want to get the innerText of <h1>
without the innerText
inside the span... this is the HTML of page:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var title = document.querySelector('div.col-md-8.info-header h1');
title = title && title.innerText;
console.log(title);
<!-- language: lang-html -->
<div class="col-md-12 header">
<div class="col-md-8 info-header">
<h1> This note is for h1 tag!!!!!! <span> this note is insidespan v226hql!!! </span>
</h1>
</div>
</div>
<!-- end snippet -->
but this will return the innerText
of both <h1>
and <span>
.
what can I do?
答案1
得分: 5
一旦您选择了父元素,您将需要选择其子级文本节点,并获取该节点的内容:
const h1 = document.querySelector('div.col-md-8.info-header h1');
const text = h1.childNodes[0].textContent;
console.log(text);
<div class="col-md-12 header">
<div class="col-md-8 info-header">
<h1> This note is for h1 tag!!!!!! <span> this note is insidespan v226hql!!! </span></h1>
</div>
</div>
不幸的是,没有办法直接使用查询字符串导航到文本节点,因此您必须首先通过childNodes
进行导航。
英文:
Once you select the parent, you'll have to select its child text node, and get the contents of that node:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const h1 = document.querySelector('div.col-md-8.info-header h1');
const text = h1.childNodes[0].textContent;
console.log(text);
<!-- language: lang-html -->
<div class="col-md-12 header">
<div class="col-md-8 info-header">
<h1> This note is for h1 tag!!!!!! <span> this note is insidespan v226hql!!! </span>
</h1>
</div>
</div>
<!-- end snippet -->
Unfortunately there's no way to navigate directly to a text node with a query string, so you have to go through the childNodes
first.
答案2
得分: 1
尝试这样做。
var mainElement = document.getElementById("id_of_h1"),
innerElements = mainElement.firstChild,
innerTexts = [];
while (innerElements) {
if (innerElements.nodeType == 3) {
innerTexts.push(innerElements.data);
}
innerElements = innerElements.nextSibling;
}
var finalResult = innerTexts.join("");
finalResult将仅包含顶级元素的innerText。
英文:
Try this.
var mainElement = document.getElementById("id_of_h1"),
innerElements = mainElement.firstChild,
innerTexts = [];
while (innerElements) {
if (innerElements.nodeType == 3) {
innerTexts.push(innerElements.data);
}
innerElements = innerElements.nextSibling;
}
var finalResult = innerTexts.join("");
finaresult will contain the intertext of the top element only.
答案3
得分: 0
以下是翻译好的部分:
"如果您有<h1>hello <span>another</span> world
,并且需要获取除HTML元素之外的所有文本- hello world
而不是hello another world
,那么您需要按照这种方式进行操作。"
const h1 = document.querySelector('div.col-md-8.info-header h1');
const el = h1.childNodes;
let result = "";
for(i=0;i<el.length;i++){
if(el[i].nodeName == '#text'){
result+=el[i].textContent;
}
}
console.log(result);
<div class="col-md-12 header">
<div class="col-md-8 info-header">
<h1> This note is for h1 tag!!!!!! <span> this note is insidespan v226hql!!! </span> extra text without tag
</h1>
</div>
</div>
英文:
In case you have <h1>hello <span>another</span> world
and need to get all text except html elements- hello world
not hello another world
,then you need to this way
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const h1 = document.querySelector('div.col-md-8.info-header h1');
const el = h1.childNodes;
let result = "";
for(i=0;i<el.length;i++){
if(el[i].nodeName == '#text'){
result+=el[i].textContent;
}
}
console.log(result);
<!-- language: lang-html -->
<div class="col-md-12 header">
<div class="col-md-8 info-header">
<h1> This note is for h1 tag!!!!!! <span> this note is insidespan v226hql!!! </span> extra text without tag
</h1>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论