我想基于HR标签分割我从不同DIV中的Web服务收到的文章(= HTML内容)。
我用一个例子来解释,这是我从服务中得到的:
<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>
我想根据HR标签制作:
<div><p>This is an article bla bla</p></div>
<div><p>this is the next page of the article blabla ...</p></div>
我尝试了不同的想法,但是没有用。如何使用Javascript或JQuery(或使用其他方法; - ))?
答案 0 :(得分:6)
使用split创建和数组并循环创建div。
var html = "<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>";
$(html.split('<hr/>')).each(function(){
$('#test').append('<div>'+this+'</div>')
})
答案 1 :(得分:1)
尝试 -
var html = '<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>'
var htmlarr = html.split('<hr/>');
html = html.replace(/<hr\/>/g,'');
$('body').append(html);
这将删除所有<hr/>
,然后将剩余的contnet附加到页面。我认为如果不执行split
,我将实现您想要的目标。你可以做这样的分裂 -
var htmlarr = html.split('<hr/>');
这将为您留下一个数组,其中包含您在问题中列出的两位HTML。您可以使用 -
将其添加到您的页面$.each(htmlarr,function (index,value) {
$('body').append(value);
});