在p标签中包装一组没有标记和随机的段落

时间:2015-09-01 00:11:22

标签: javascript

我正在研究传统API的主题,这些主题API不会很好/如果没有标记/标记。我已经能够用wrap()和其他方法将其中的大部分重新塑造成形状,但由于某种原因,这个让我很难受。我是否真的需要为这样的事情找出for循环 - 或者是否有一种廉价而肮脏的方式将这些段落放入<p&gt;段落?

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

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

2 个答案:

答案 0 :(得分:0)

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

$("#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">

答案 1 :(得分:0)

如果你要做很多事,你应该调查Regex。您可以更好地控制文本,代码很短。

我在innerHTML上调用.match并使用此正则表达式字符串/".+/g。它会从最左边的引号中找到所有文本,直到换行符。

text.match(/".+/g);

以下是匹配解析的方式。 https://regex101.com/r/mP7pU9/1

这就是它在jQuery中的样子。 http://jsfiddle.net/jg7cxpy4/2/