最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Split HTML in divs with JavascriptJQuery based on HR tags - Stack Overflow

programmeradmin7浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

Use 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

发布评论

评论列表(0)

  1. 暂无评论