如何在JavaScript中获取h1元素的innerText,而不包括其子元素的innerText?

huangapple go评论69阅读模式
英文:

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 &lt;h1&gt; 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(&#39;div.col-md-8.info-header h1&#39;);
title = title &amp;&amp; title.innerText;
console.log(title);

<!-- language: lang-html -->

&lt;div class=&quot;col-md-12 header&quot;&gt;
  &lt;div class=&quot;col-md-8 info-header&quot;&gt;
    &lt;h1&gt; This note is for h1 tag!!!!!! &lt;span&gt; this note is insidespan v226hql!!! &lt;/span&gt;
    &lt;/h1&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

but this will return the innerText of both &lt;h1&gt; and &lt;span&gt;.

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(&#39;div.col-md-8.info-header h1&#39;);
const text = h1.childNodes[0].textContent;
console.log(text);

<!-- language: lang-html -->

&lt;div class=&quot;col-md-12 header&quot;&gt;
  &lt;div class=&quot;col-md-8 info-header&quot;&gt;
    &lt;h1&gt; This note is for h1 tag!!!!!! &lt;span&gt; this note is insidespan v226hql!!! &lt;/span&gt;
    &lt;/h1&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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(&quot;id_of_h1&quot;),
    innerElements = mainElement.firstChild,
    innerTexts = [];

while (innerElements) {
    if (innerElements.nodeType == 3) {
        innerTexts.push(innerElements.data);
    }
    innerElements = innerElements.nextSibling;
}

var finalResult = innerTexts.join(&quot;&quot;);

finaresult will contain the intertext of the top element only.

答案3

得分: 0

以下是翻译好的部分:

"如果您有&lt;h1&gt;hello &lt;span&gt;another&lt;/span&gt; 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 &lt;h1&gt;hello &lt;span&gt;another&lt;/span&gt; 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(&#39;div.col-md-8.info-header h1&#39;);
const el = h1.childNodes;
let result = &quot;&quot;;
for(i=0;i&lt;el.length;i++){
   if(el[i].nodeName == &#39;#text&#39;){
     result+=el[i].textContent;
   }
}
console.log(result);

<!-- language: lang-html -->

&lt;div class=&quot;col-md-12 header&quot;&gt;
  &lt;div class=&quot;col-md-8 info-header&quot;&gt;
    &lt;h1&gt; This note is for h1 tag!!!!!! &lt;span&gt; this note is insidespan v226hql!!! &lt;/span&gt; extra text without tag
    &lt;/h1&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 15:10:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定