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

标签 javascript html semantics semantic-markup

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

bla bla bla long paragraph here
<br/>
<br/>
bla bla bla more paragraph text
<br/>
<br/>

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

<p>
  bla bla bla long paragraph here
</p>
<p>
  bla bla bla more paragraph text
</p>

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

我在想我可能会尝试编写一个正则表达式,但在我这样做之前我想确保我是 a) 避免受伤的世界和 b) 那里没有其他东西 - 我会尝试进行谷歌搜索,但还没有找到任何结果。

感谢您的任何建议!

最佳答案

我很无聊。我确定需要进行优化/调整。使用一点点 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();
  });

这是我测试的 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>

结果:

<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>

关于javascript - 有没有一种简单的方法可以将带有多个 <br/> 标记的 HTML 转换为 Javascript 中适当的周围 <p> 标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1275250/

相关文章:

Javascript字符全部替换

javascript - 无法使用 apply() 调用 Backbone trigger() 方法

javascript - Chart.js/Angular-Chart 线外部样式问题

html - 如何在HTML5中不使用对等连接的情况下实现多方音频 session 系统?

html - 对齐两个垂直的 HTML 表格

android - Material 设计 : Button or Edit Text?

c++ - 具有范围限定符的模板语法的含义

java - 语义和语法上冗余的花括号更改编译器错误消息

javascript - 我可以使用javascript调用delphi xe8 REST服务post吗?

javascript - 将垂直制表符转换为 Accordion