如何在HTML中将<li>标签替换为另一个标签并将其值放入新标签中。

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

How to replace <li> tag in HTML with another tag and place it value in the new tag

问题

Sure, here is the translated HTML code with the &lt;li&gt; elements wrapped as requested:

  1. &lt;ul&gt;&lt;li&gt;&lt;li-text&gt;one&lt;/li-text&gt;&lt;/li&gt;&lt;li&gt;&lt;li-text&gt;two&lt;/li-text&gt;&lt;ul&gt;&lt;li&gt;&lt;li-text&gt;three&lt;/li-text&gt;&lt;ul&gt;&lt;li&gt;&lt;li-text&gt;four&lt;/li-text&gt;&lt;ul&gt;&lt;li&gt;&lt;li-text&gt;five&lt;/li-text&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;

I've wrapped each &lt;li&gt; element with &lt;li-text&gt;{li value}&lt;/li-text&gt; as you requested.

英文:

I need to update the following HTML to wrap each &lt;li&gt; with &lt;li&gt;&lt;li-text&gt;{li value}&lt;/li-text&gt;

  1. &lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;ul&gt;&lt;li&gt;three&lt;ul&gt;&lt;li&gt;four&lt;ul&gt;&lt;li&gt;five&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;

hints: I ONLY USING JS, NO JQUERY

I'm trying this, but it's not working as expected!

  1. var html = `&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;ul&gt;&lt;li&gt;three&lt;ul&gt;&lt;li&gt;four&lt;ul&gt;&lt;li&gt;five&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;`.replace(/&lt;li[^&gt;]*&gt;/g,&#39;&lt;li&gt;&lt;li-text&gt;$&amp;&lt;/li-text&gt;&#39;)

The result I'm getting is not correct! the value of list should be between &lt;li-text&gt;&lt;/li-text&gt; tags!

  1. &lt;ul&gt;&lt;li&gt;&lt;li-text&gt;&lt;li&gt;&lt;/li-text&gt;one&lt;/li&gt;&lt;li&gt;&lt;li-text&gt;&lt;li&gt;&lt;/li-text&gt;two&lt;ul&gt;&lt;li&gt;&lt;li-text&gt;&lt;li&gt;&lt;/li-text&gt;three&lt;ul&gt;&lt;li&gt;&lt;li-text&gt;&lt;li&gt;&lt;/li-text&gt;four&lt;ul&gt;&lt;li&gt;&lt;li-text&gt;&lt;li&gt;&lt;/li-text&gt;five&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;

答案1

得分: 1

忘记自己解析HTML吧。您可以使用浏览器内置的解析器,选择您的li元素,并替换要包装在li-text中的内容。

  1. const input = `<ul><li>one</li><li>two<ul><li>three<ul><li>four<ul><li>five</li></ul></li></ul></li></ul></li></ul>`
  2. const parser = new DOMParser()
  3. const doc = parser.parseFromString(input, "text/html")
  4. doc.querySelectorAll("li").forEach(li => li.innerHTML = `<li-text>${li.innerHTML}</li-text>`)
  5. const output = doc.body.innerHTML
  6. console.log(output)
英文:

Forget about parsing HTML yourself. You can use the browser's built-in parser, select your li elements, and replace the content to be wrapped with li-text.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const input = `&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;ul&gt;&lt;li&gt;three&lt;ul&gt;&lt;li&gt;four&lt;ul&gt;&lt;li&gt;five&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;`
  2. const parser = new DOMParser()
  3. const doc = parser.parseFromString(input, &quot;text/html&quot;)
  4. doc.querySelectorAll(&quot;li&quot;).forEach(li =&gt; li.innerHTML = `&lt;li-text&gt;${li.innerHTML}&lt;/li-text&gt;`)
  5. const output = doc.body.innerHTML
  6. console.log(output)

<!-- end snippet -->

答案2

得分: 1

解析内容为HTML,选择所有<li>元素,然后用包装器替换每个直接子级文本节点:

  1. function addWrapperToLi(html) {
  2. const parser = new DOMParser();
  3. const parsed = parser.parseFromString(html, 'text/html');
  4. const textNodes = [...parsed.querySelectorAll('li')].flatMap(
  5. // .childNodes 包含各种节点,包括元素
  6. element => [...element.childNodes].filter(
  7. // ...但我们只想要文本节点。
  8. node => node.nodeType === Node.TEXT_NODE
  9. )
  10. );
  11. textNodes.forEach(node => {
  12. const wrapper = document.createElement('li-text');
  13. wrapper.innerText = node.textContent;
  14. node.parentElement.replaceChild(wrapper, node);
  15. });
  16. return parsed.body.innerHTML;
  17. }

尝试它:

  1. <!-- begin snippet: js hide: true -->
  2. <!-- language: lang-js -->
  3. console.config({ maximize: true });
  4. function addWrapperToLi(html) {
  5. const parser = new DOMParser();
  6. const parsed = parser.parseFromString(html, 'text/html');
  7. const textNodes = [...parsed.querySelectorAll('li')].flatMap(
  8. element => [...element.childNodes].filter(
  9. node => node.nodeType === Node.TEXT_NODE
  10. )
  11. );
  12. textNodes.forEach(node => {
  13. const wrapper = document.createElement('li-text');
  14. wrapper.innerText = node.textContent;
  15. node.parentElement.replaceChild(wrapper, node);
  16. });
  17. return parsed.body.innerHTML;
  18. }
  19. const testcases = [
  20. '<ul><li>one</li><li>two<ul><li>three<ul><li>four<ul><li>five</li></ul></li></ul></li></ul></li></ul></li></ul>',
  21. '<ul><li>one</li><li>two<ul><li>three<ul><li>four<ul><li>five</li></ul>after four</li></ul></li></ul></li></ul>after two</li></ul>'
  22. ];
  23. testcases.forEach(
  24. testcase => console.log(addWrapperToLi(testcase))
  25. );
  26. <!-- language: lang-html -->
  27. <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
  28. <!-- end snippet -->
英文:

Parse the content as HTML, select all &lt;li&gt; elements, then replace each direct-children text node with a wrapper:

  1. function addWrapperToLi(html) {
  2. const parser = new DOMParser();
  3. const parsed = parser.parseFromString(html, &#39;text/html&#39;);
  4. const textNodes = [...parsed.querySelectorAll(&#39;li&#39;)].flatMap(
  5. // .childNodes contains all kinds of nodes, including elements
  6. element =&gt; [...element.childNodes].filter(
  7. // ...but we only want text nodes.
  8. node =&gt; node.nodeType === Node.TEXT_NODE
  9. )
  10. );
  11. textNodes.forEach(node =&gt; {
  12. const wrapper = document.createElement(&#39;li-text&#39;);
  13. wrapper.innerText = node.textContent;
  14. node.parentElement.replaceChild(wrapper, node);
  15. });
  16. return parsed.body.innerHTML;
  17. }

Try it:

<!-- begin snippet: js hide: true -->

<!-- language: lang-js -->

  1. console.config({ maximize: true });
  2. function addWrapperToLi(html) {
  3. const parser = new DOMParser();
  4. const parsed = parser.parseFromString(html, &#39;text/html&#39;);
  5. const textNodes = [...parsed.querySelectorAll(&#39;li&#39;)].flatMap(
  6. element =&gt; [...element.childNodes].filter(
  7. node =&gt; node.nodeType === Node.TEXT_NODE
  8. )
  9. );
  10. textNodes.forEach(node =&gt; {
  11. const wrapper = document.createElement(&#39;li-text&#39;);
  12. wrapper.innerText = node.textContent;
  13. node.parentElement.replaceChild(wrapper, node);
  14. });
  15. return parsed.body.innerHTML;
  16. }
  17. const testcases = [
  18. &#39;&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;ul&gt;&lt;li&gt;three&lt;ul&gt;&lt;li&gt;four&lt;ul&gt;&lt;li&gt;five&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&#39;,
  19. &#39;&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;ul&gt;&lt;li&gt;three&lt;ul&gt;&lt;li&gt;four&lt;ul&gt;&lt;li&gt;five&lt;/li&gt;&lt;/ul&gt;after four&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;after two&lt;/li&gt;&lt;/ul&gt;&#39;
  20. ];
  21. testcases.forEach(
  22. testcase =&gt; console.log(addWrapperToLi(testcase))
  23. );

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

  1. &lt;script src=&quot;https://gh-canon.github.io/stack-snippet-console/console.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月13日 08:03:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460952.html
匿名

发表评论

匿名网友

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

确定