简体   繁体   English

将 p 标签添加到无格式文本 - 纯 JavaScript

[英]Add p tag to unformatted text - Plain JavaScript

I have a content which inner elements text doesn't wrap with any html tags.我有一个内部元素文本没有用任何 html 标签包装的内容。 How can I add a “p” tag using plain JavaScript?如何使用纯 JavaScript 添加“p”标签? jQuery solution is already existed but I need plain JavaScript solution. jQuery 解决方案已经存在,但我需要纯 JavaScript 解决方案。 I've already tried with “nodeType” , “innerText” , “textContent” and “innerHTML” .我已经尝试过“nodeType”“innerText”“textContent”“innerHTML”

jQuery solution is jQuery 解决方案是

$('.arfaa').each(function() { 
$(this).contents().filter(function() {
return this.nodeType === 3;  
}).wrap('<p></p>');
});

How to figure this out by plain JavaScript?如何通过纯 JavaScript 来解决这个问题?

<div class="content">

This is just a line of text.

<div><img src="demo.jpg" alt=""></div>

This is just a line of text.

<br><br>

<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>

<br><br>

This is just a <a href="#">line</a> of text.

<br><br>    

This is just a <span>line of text</span>.

<br><br>

<figure><img src="demo.jpg" alt="text">
<figcaption>img description</figcaption>
</figure>

This is just a line of text.

<br><br>

This is just a line of text.

<table><tbody>
<tr><th>A</th><td>A value</td></tr>
<tr><th>B</th><td>B value</td></tr></tbody>
</table>

<br><br>

This is just a line of text.

</div>

Expected output预期输出

<div class="content">

<p>This is just a line of text.</p>

<div><img src="demo.jpg" alt=""></div>

<p>This is just a line of text.</p>

<br><br>

<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>

<br><br>

<p>This is just a <a href="#">line</a> of text.</p>

<br><br>    

<p>This is just a <span>line of text</span>.</p>

<br><br>

<figure><img src="demo.jpg" alt="text">
<figcaption>img description</figcaption>
</figure>

<p>This is just a line of text.</p>

<br><br>

<p>This is just a line of text.</p>

<table><tbody>
<tr><th>A</th><td>A value</td></tr>
<tr><th>B</th><td>B value</td></tr></tbody>
</table>

<br><br>

<p>This is just a line of text.</p>

</div>

This is one way to do it in pure javascript.这是在纯 javascript 中执行此操作的一种方法。 Use the findTextNodesFlat to replace the Each function.使用 findTextNodesFlat 替换 Each 函数。 If you want to get all texts nodes (independent of its depth), use the findTextNodesTree instead.如果要获取所有文本节点(与其深度无关),请改用 findTextNodesTree。 Then wrap the textnodes using the wrapTextNodes function.然后使用 wrapTextNodes 函数包装文本节点。

// walks the child elements of a dom element and saves textnodes to the textNodes array
var findTextNodesFlat = function (node, textNodes) {
    node = node.firstChild;
    while(node) {
        if (node.nodeType == Node.TEXT_NODE)
            textNodes.push(node);

        node = node.nextSibling;
    }
};

// walks the dom element recursively and saves textnodes to the textNodes array
var findTextNodesTree = function (node, textNodes) {
    if (node.nodeType == Node.TEXT_NODE)
        textNodes.push(node);

    node = node.firstChild;
    while(node) {
        findTextNodes(node, textNodes);
        node = node.nextSibling;
    }
};

// wraps the textNodes elements with <p></p>
var wrapTextNodes = function (textNodes) {
    for(let node of textNodes) {
        let parentNode = node.parentNode;
        let wrapper = document.createElement('p');
        parentNode.insertBefore(wrapper, node);
        wrapper.appendChild(node);
    }
};

var textNodes = [];
findTextNodesFlat(document.getElementsByClassName("content")[0], textNodes);
wrapTextNodes(textNodes);

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

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