get text of an element without children in javascript

后端 未结 4 587
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 18:47

How do I get the text of an element without the children? Neither element.textContent nor element.innerText seem to be working.

HTML:

4条回答
  •  心在旅途
    2020-12-17 19:27

    The text of an element is also a separate node. Consider this piece of code:

    
        Some text
        Inner text
        More text
        More inner text
        Even more text
    
    

    What do you mean now when you say you want the text of the element? Just the direct children?

    Then this piece code of code may help:

    for (var element in elements) {
        if (element.nodeType == Node.TEXT_NODE) {
            // do something
        }
    }
    

提交回复
热议问题