admin管理员组文章数量:1328022
I want to split articles (= HTML content) that I receive from a webservice in different DIVs, based on a HR tag.
I explain with an example, this is what I receive from the service:
<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>
I want to make, based on the HR-tag:
<div><p>This is an article bla bla</p></div>
<div><p>this is the next page of the article blabla ...</p></div>
I tried different thinks but is doesn't work. How can I do that with Javascript or JQuery (or with another method ;-))?
I want to split articles (= HTML content) that I receive from a webservice in different DIVs, based on a HR tag.
I explain with an example, this is what I receive from the service:
<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>
I want to make, based on the HR-tag:
<div><p>This is an article bla bla</p></div>
<div><p>this is the next page of the article blabla ...</p></div>
I tried different thinks but is doesn't work. How can I do that with Javascript or JQuery (or with another method ;-))?
Share Improve this question edited Nov 12, 2011 at 20:04 Adam Rackis 83.4k57 gold badges278 silver badges400 bronze badges asked Nov 12, 2011 at 8:48 benskebenske 4,0504 gold badges26 silver badges22 bronze badges2 Answers
Reset to default 6Use split to create and array and loop through to create the divs.
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>')
})
Demo
Try -
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);
This will just remove all the <hr/>
s then append the remaining contnet to the page.Which I think will achieve what you want without performing a split
. You could do a split like this -
var htmlarr = html.split('<hr/>');
Which would leave you with an array containing the two bits of HTML you list in your question. You could add this to your page using -
$.each(htmlarr,function (index,value) {
$('body').append(value);
});
Demo - http://jsfiddle/cCR34/2
本文标签: Split HTML in divs with JavascriptJQuery based on HR tagsStack Overflow
版权声明:本文标题:Split HTML in divs with JavascriptJQuery based on HR tags - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742237113a2438281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论