有没有一种简单的方法可以将带有多个<br/>标签的HTML转换为Javascript中适当的周围<p>标签?

Ruf*_*hez 3 html javascript semantic-markup semantics

假设我有一堆HTML,如下所示:

bla bla bla long paragraph here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>
Run Code Online (Sandbox Code Playgroud)

有一个简单的方法使用Javascript将其转换为正确的语义<p>标签吗?例如:

<p>
  bla bla bla long paragraph here
</p>
<p>
  bla bla bla more paragraph text
</p>
Run Code Online (Sandbox Code Playgroud)

输出间距并不重要,理想情况下它可以用于任何输入间距.

我想我可能会尝试制作一个正则表达式,但在我这样做之前,我想确保我是a)避免受伤的世界b)那里没有别的东西 - 我试图做谷歌搜索,但尚未提出任何建议.

谢谢你的建议!

gna*_*arf 6

我无聊.我确信需要进行优化/调整.使用一点点jQuery来实现它的魔力.在FF3工作.你的问题的答案是,有一个非常"简单"的方式:)

$(function() {
  $.fn.pmaker = function() {
    var brs = 0;
    var nodes = [];

    function makeP()
    {
      // only bother doing this if we have nodes to stick into a P
      if (nodes.length) {
        var p = $("<p/>");
        p.insertBefore(nodes[0]);  // insert a new P before the content
        p.append(nodes); // add the children        
        nodes = [];
      }
      brs=0;
    }

    this.contents().each(function() {    
      if (this.nodeType == 3) // text node 
      {
        // if the text has non whitespace - reset the BR counter
        if (/\S+/.test(this.data)) {
          nodes.push(this);
          brs = 0;
        }
      } else if (this.nodeType == 1) {
        if (/br/i.test(this.tagName)) {
          if (++brs == 2) {
            $(this).remove(); // remove this BR from the dom
            $(nodes.pop()).remove(); // delete the previous BR from the array and the DOM
            makeP();
          } else {
            nodes.push(this);
          }
        } else if (/^(?:p)$/i.test(this.tagName)) {
          // these tags for the P break but dont scan within
          makeP();
        } else if (/^(?:div)$/i.test(this.tagName)) {
          // force a P break and scan within
          makeP();
          $(this).pmaker();
        } else {
          brs = 0; // some other tag - reset brs.
          nodes.push(this); // add the node 
          // specific nodes to not peek inside of - inline tags
          if (!(/^(?:b|i|strong|em|span|u)$/i.test(this.tagName))) {
            $(this).pmaker(); // peek inside for P needs            
          }
        } 
      } 
    });
    while ((brs--)>0) { // remove any extra BR's at the end
      $(nodes.pop()).remove();
    }
    makeP();
    return this;
  };

  // run it against something:
  $(function(){ 
    $("#worker").pmaker();
  });
Run Code Online (Sandbox Code Playgroud)

这是我测试过的html部分:

<div id="worker">
bla bla bla long <b>paragraph</b> here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>
this text should end up in a P
<div class='test'>
  and so should this
  <br/>
  <br/>
  and this<br/>without breaking at the single BR
</div>
and then we have the a "buggy" clause
<p>
  fear the real P!
</p>
and a trailing br<br/>
</div>
Run Code Online (Sandbox Code Playgroud)

结果如下:

<div id="worker"><p>
bla bla bla long <b>paragraph</b> here
</p>
<p>
bla bla bla more paragraph text
</p>
<p>
this text should end up in a P
</p><div class="test"><p>
  and so should this
  </p>
  <p>
  and this<br/>without breaking at the single BR
</p></div><p>
and then we have the a "buggy" clause
</p><p>
  fear the real P!
</p><p>
and a trailing br</p>
</div>
Run Code Online (Sandbox Code Playgroud)


Rob*_*rog 5

扫描每个子元素 + 封闭元素的文本。每次遇到“br”元素时,创建一个“p”元素,并将所有待处理的内容附加到它。起泡,冲洗,重复。

不要忘记删除您要重新定位到新“p”元素的内容。

我发现这个库 (prototype.js)对这类事情很有用。