简体   繁体   English

如何在元素P中获取文本,用javascript DOM排除它们可能的子元素A?

[英]How can I get the text inside an element P excluding their possible child elements A with javascript DOM?

I want to get the text (not plain-text, but formatted as well) inside each paragraph excluding their possible links. 我想在每个段落中获取文本(不是纯文本,但也是格式化的),不包括它们可能的链接。 Please, explain to me in pure javascript, not jQuery. 请用纯javascript向我解释,而不是jQuery。

*I wouldn't like to use neither IDs nor the method getElementById(). *我不想既不使用ID也不使用方法getElementById()。

Example: 例:

 for (i = 0; i < document.getElementsByTagName('p').length; i++){ p = document.getElementsByTagName('p')[i]; document.write(p.innerHTML); /* Here I have the text of each paragraph but also with the links*/ } 
 <p>This is a <a href="https://www.google.com">first</a> search engine.</p> <div>Here I have also some text</div> <p>This is a <b>web browser</b>.</p> <p>This is another <a href="https://www.bing.com">second</a> search engine.</p> 

The result I want should be: 我想要的结果应该是:

"This is a search engine.This is a web browser .This is another search engine." “这是一个搜索引擎。这是一个网络浏览器 。这是另一个搜索引擎。”

What about this way: 这样怎么样:

  1. Find all 'p' elements. 查找所有“ p”元素。
  2. Loop for each element looking inside iterated element for anchors. 为每个元素循环查看锚点的迭代元素。
  3. If found, remove from DOM. 如果找到,请从DOM中删除。

 const textElements = document.querySelectorAll('p'); textElements.forEach(function (element) { element.querySelectorAll('a').forEach(function (anchor) { anchor.remove(); }); }); 
  <p>This is a <a href="https://www.google.com">first</a> search engine.</p> <div>Here I have also some text</div> <p>This is a <b>web browser</b>.</p> <p>This is another <a href="https://www.bing.com">second</a> search engine.</p> 

Snippet with for loops and document.getElementsByTag 带for循环和document.getElementsByTag的代码段

 const textElements = document.getElementsByTagName('p'); for (var i = 0; i < document.getElementsByTagName('p').length; i++) { // get next p tag var someParagraphElement = document.getElementsByTagName('p')[i]; // length of tag inside paragraph var lenghtOfA = someParagraphElement.getElementsByTagName('a').length; if(lenghtOfA){ for (var j = 0; j < lenghtOfA; j++) { // a tag inside p var aTag = someParagraphElement.getElementsByTagName('a')[j]; aTag.remove(); } } } 
 <p>This is a <a href="https://www.google.com">first</a> search engine.</p> <div>Here I have also some text</div> <p>This is a <b>web browser</b>.</p> <p>This is another <a href="https://www.bing.com">second</a> search engine.</p> 

You can just make an off-DOM copy of the elements, remove the anchor elements from those, then ask for textContent : 您可以只复制元素的DOM,然后从其中删除锚元素,然后索取textContent

 let container = document.createElement('div'); [...document.getElementsByTagName('p')].forEach(p => container.innerHTML += p.innerHTML); // remove links [...container.querySelectorAll('*')].forEach(el => { if (el.tagName === 'A') el.remove(); }) console.log(container.innerHTML); document.getElementById('result').innerHTML = container.innerHTML; 
 <p>This is a <a href="https://www.google.com">first</a> search engine.</p> <div>Here I have also some text</div> <p>This is a <b>web browser</b>.</p> <p>This is another <a href="https://www.bing.com">second</a> search engine.</p> <div id="result"></div> 

Here's basically the same solution in ECMAScript 5, which you might be more used to: 这里基本上是ECMAScript 5中的相同解决方案,您可能更习惯于:

 // create a container element not in the DOM to copy stuff to var container = document.createElement('div'); // get all paragraph elements var paras = document.getElementsByTagName('p'); // iterate over each paragraph and add its innerHTML to the container for (var i = 0; i < paras.length; i++) { container.innerHTML += paras[i].innerHTML } // get all container child elements var containerChildren = container.querySelectorAll('*'); // iterate over them and remove any links for (var j = 0; j < containerChildren.length; j++) { if (containerChildren[j].tagName === 'A') containerChildren[j].remove(); } console.log(container.innerHTML); // make the result visible document.getElementById('result').innerHTML = container.innerHTML; 
 <p>This is a <a href="https://www.google.com">first</a> search engine.</p> <div>Here I have also some text</div> <p>This is a <b>web browser</b>.</p> <p>This is another <a href="https://www.bing.com">second</a> search engine.</p> <div id="result"></div> 

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

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