javascript 有没有办法只获取顶部元素的innerText(并忽略子元素的innerText)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9340449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to get innerText of only the top element (and ignore the child element's innerText)?
提问by ivymike
Is there a way to get innerText of only the top element (and ignore the child element's innerText) ?
有没有办法只获取顶部元素的innerText(并忽略子元素的innerText)?
Example:
例子:
<div>
top node text
<div> child node text </div>
</div>
How to get the "top node text" while ignoring "child node text" ? innerText property of top div seem to return concatenation of both inner , top text.
如何在忽略“子节点文本”的同时获取“顶部节点文本”?top div 的innerText 属性似乎返回inner 和top 文本的串联。
回答by Tim Down
Just iterate over the child nodes and concatenate text nodes:
只需遍历子节点并连接文本节点:
var el = document.getElementById("your_element_id"),
child = el.firstChild,
texts = [];
while (child) {
if (child.nodeType == 3) {
texts.push(child.data);
}
child = child.nextSibling;
}
var text = texts.join("");
回答by Rob W
- Clone the element.
- Loop through all child nodes (backwards, to avoid conflicts):
If the element has atagName
attribute, then it's an element: Remove the node. - Use
innerText
to get the textual contents (with fallback totextContent
, wheninnerText
is not supported).
- 克隆元素。
- 循环遍历所有子节点(向后,以避免冲突):
如果元素具有tagName
属性,则它是一个元素:删除节点。 - 使用
innerText
来获取文本内容(回退到textContent
时innerText
不支持)。
Code:
代码:
var elem = document.getElementById('theelement');
elem = elem.cloneNode(true);
for (var i=elem.childNodes.length-1; i>=0; i--) {
if (elem.childNodes[i].tagName) elem.removeChild(elem.childNodes[i]);
}
var innerText = elem['innerText' in elem ? 'innerText' : 'textContent'];
回答by sensor
This will work in your example:
document.getElementById("item").firstChild.nodeValue;
这将适用于您的示例:
document.getElementById("item").firstChild.nodeValue;
Note: Keep in mind that this will work if you know you are dealing with that specific HTML. If your HTML can change, for example to:
注意:请记住,如果您知道您正在处理特定的 HTML,这将起作用。如果您的 HTML 可以更改,例如:
<div>
<div class="item"> child node text </div>
top node text
</div>
then you should use the more generic solution by @Tim Down
那么你应该使用@Tim Down 提供的更通用的解决方案
Here is working code snippet:
这是工作代码片段:
window.onload = function() {
var text = document.getElementById("item").firstChild.nodeValue;
document.getElementById("result").innerText = text.trim();
};
#result {
border: 1px solid red;
}
<div id="item">
top node text
<div> child node text </div>
</div>
<strong>Result:</strong> <div id="result"></div>
回答by ehsaneha
function getDirectInnerText(element) {
var childNodes = element.childNodes;
result = '';
for (var i = 0; i < childNodes.length; i++) {
if(childNodes[i].nodeType == 3) {
result += childNodes[i].data;
}
}
return result;
}
element = document.querySelector("div#element");
console.log(getDirectInnerText(element))
<div id="element">
top node text
<div> child node text </div>
</div>
回答by johnwcb
If you don't want to ignore the child element's inner text, use the following function:
如果不想忽略子元素的内部文本,请使用以下函数:
function getInnerText(el) {
var x = [];
var child = el.firstChild;
while (child) {
if (child.nodeType == 3) {
x.push(child.nodeValue);
}
else if (child.nodeType == 1) {
var ii = getInnerText(child);
if (ii.length > 0) x.push(ii);
}
child = child.nextSibling;
}
return x.join(" ");
}