javascript - 将一组没有标记且随机 <br> 的段落包装在 p 标签中

标签 javascript

我正在为一个遗留 API 开发主题,它的结果不太精彩/如果不是无标记/标记的话。我已经能够通过wrap()将大部分恢复到原来的状态。和其他方法,但由于某种原因,这个方法难倒了我。我真的需要摆脱这样的 for 循环吗?还是有一种廉价而肮脏的方法将这些段落放入 <p 中? > 段落?

<div class="bio">
    "Some Ember is the new solo project of Man/Miracle's Dylan Travis. "Era of Wind" is only the second song he's released, it is icy and desolate. It's a little bit Lovecraft (From fingertip to dilated eye/ it closes grip on vibrated flesh, Travis sings) and a little bit Fever Ray, propelled by haunted, droning bass. I keep thinking about this lonely guy in a lake, like a 19th century convict rowing through fog, making his great escape from an island prison and sinking halfway to shore. What happens to that guy? What if he had a Moog to play on the way down? " --The Fader
    <br>
    <br>
    "Like some kind of abridged soundtrack to Bergman's Persona pared down to its abstract purity, this ten minute mood piece charts a psychic journey through dissolution and restructure, opening with a searing purge of the sonic palate, only to find itself wading through the aftermath's fog of disarray. Taking cues from the likes of Mazzy Star, Happy New Year and Grouper, the San Francisco duo immerse themselves in a narcotic mixture of sensuality and obliqueness, mostly leaving meaning and catharsis to hover just out of reach while an ominous air of gloom takes hold." --Sonic Masala
</div>

感觉尝试让这些形状可能不值得。想法?

最佳答案

也许您可以使用 DOM 中的 childNode 来实现这一点。

$("#btnConvert").on("click",function(){
	var text = $("#text");
     result = $("<div>").html(text.val()).children().contents().filter(function() {
  return this.nodeType == 3 && this.data.trim() != "";
}).map(function(){ return "<p>"+this.data+"</p>"}).get().join("");
    text.val(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p>value</p>
<textarea name="text" id="text" cols="30" rows="10">
    <div class="bio">
    "Some Ember is the new solo project of Man/Miracle's Dylan Travis. "Era of Wind" is only the second song he's released, it is icy and desolate. It's a little bit Lovecraft (From fingertip to dilated eye/ it closes grip on vibrated flesh, Travis sings) and a little bit Fever Ray, propelled by haunted, droning bass. I keep thinking about this lonely guy in a lake, like a 19th century convict rowing through fog, making his great escape from an island prison and sinking halfway to shore. What happens to that guy? What if he had a Moog to play on the way down? " --The Fader
    <br>
    <br>
    "Like some kind of abridged soundtrack to Bergman's Persona pared down to its abstract purity, this ten minute mood piece charts a psychic journey through dissolution and restructure, opening with a searing purge of the sonic palate, only to find itself wading through the aftermath's fog of disarray. Taking cues from the likes of Mazzy Star, Happy New Year and Grouper, the San Francisco duo immerse themselves in a narcotic mixture of sensuality and obliqueness, mostly leaving meaning and catharsis to hover just out of reach while an ominous air of gloom takes hold." --Sonic Masala
</div>
</textarea>
<br>
<input type="button" id="btnConvert" name="btnConvert" value="Convert">

关于javascript - 将一组没有标记且随机 <br> 的段落包装在 p 标签中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32321696/

相关文章:

javascript - 如何制作响应式文本

javascript - execCommand ('copy' ) 无法以编程方式工作,仅当通过 DevTools 控制台执行时

javascript - MongoDB JSON 中没有引号

javascript - 从 javascript/jquery 中的 formData 中删除特定文件

javascript - 嵌套查询 - 未定义错误

javascript - 位置置顶

javascript - toLocaleDateString() - 挪威语问题

javascript - 在内容脚本和主附加脚本之间传递 RegExp 对象

javascript - Markdown 子集

javascript - VideoJS 事件在 Firefox 中启动时结束