简体   繁体   English

如何仅获取 Javascript 中 DOM 元素中直接包含的文本?

[英]How to get only directly contained text in DOM element in Javascript?

I have a div with some children within and it's directly contained text like below:我有一个 div 里面有一些孩子,它直接包含如下文本:

<div id="price">
   100$
   <span>some ads</span>
   <span>another ads</span>
   for each
</div>

I want to get directly contained text '100$' in this DOM element.我想在这个 DOM 元素中直接包含文本“100$”。

I tried with innerHTML , innerText and textContent .我尝试使用innerHTMLinnerTexttextContent But they show all text including children's text like below:但它们显示所有文本,包括儿童文本,如下所示:

 const element = document.getElementById('price'); console.log(element.innerText); console.log(element.innerHTML); console.log(element.textContent);
 <div id="price"> 100$ <span>some ads</span> <span>another ads</span> for each </div>

The expected result is 100$ (I don't need "for each") which is directly contained text in parent element.预期结果是100$ (我不需要“for each”),它直接包含在父元素中的文本。 So then I can get the price and its currency.所以我可以得到价格和它的货币。

note: jquery is also allowed to use.注:jquery也允许使用。

.clone() clones the selected element. .clone()克隆选定的元素。

.children() selects the children from the cloned element .children()从克隆元素中选择子元素

.remove() removes the previously selected children .remove()删除先前选择的子项

.end() selects the selected element again .end()再次选择被选元素

.text() gets the text from the element without children .text()从没有子元素的元素中获取文本

 const elementText = $("#price").clone().children().remove().end().text(); console.log(elementText.trim().split("\n")[0]);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="parent"> Parent <span>Child 1</span> <span>Child 2</span> </div> <div id="price"> 100$ <span>some ads</span> <span>another ads</span> for each </div>

EDIT: You can also use this:编辑:你也可以使用这个:

$("#price").contents().get(0).nodeValue.trim()

Filter the childNodes to include only text nodes and use textContent of each of the matching nodes:过滤childNodes以仅包含文本节点并使用每个匹配节点的textContent

const text = Array.prototype.filter
    .call(element.childNodes, (child) => child.nodeType === Node.TEXT_NODE)
    .map((child) => child.textContent)
    .join('');

The text includes the full markup of the text, including newlines. text包括文本的完整标记,包括换行符。 If this is undesired, use text.trim() .如果这是不希望的,请使用text.trim()

The filter.call is used because childNodes is a NodeList , which is array-like, but does not support .filter method.使用filter.call是因为childNodes是一个NodeList ,它类似于数组,但不支持.filter方法。


To get text only for the first node仅获取第一个节点的文本

const text = Array.prototype.filter
    .call(element.childNodes, (child) => child.nodeType === Node.TEXT_NODE)[0];

Alternatively, if you can rely on the fact that the value is always the first child, the above can be simplified to或者,如果您可以依赖该值始终是第一个孩子这一事实,则上述内容可以简化为

const text = element.childNodes[0];

With jQuery:使用 jQuery:

const text = $($('#price').contents()[0]).text().trim();

You can do this using the "outerText" property.您可以使用“outerText”属性执行此操作。 Like in the example below:如下例所示:

 var text = document.querySelector('body').outerText; var regex = /([\d]+)\$/g; console.log(text.match(regex).join());
 <div id="price"> 100$ <span>some ads</span> <span>another ads</span> for each </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM