简体   繁体   中英

How can I cut a javascript array in half but to the nearest closing </p> or </h1> tag?

I can get it cut in half, I'm just not sure how to go about making sure half goes until the next closing block level tag.

Fake Html:

<div class="content-item">    
<h1>Title</h1>
<p>Pellentesque nec erat diam. Donec condimentum tortor nec tellus gravida uscipit</p> 
<h1>Title</h1>
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus</p>
</div>

Javascript so far:

var splitChar = " ";
var wordsSplit = $(".content-item").html().split(splitChar);
var wordsCount = wordsSplit.length;
var half = Math.floor(wordsCount / 2);

Just iterate forward through the array until you hit the end or an closing block


i = half;
while (!wordsSplit[i++].match(/<\//) && i < wordsSplit.length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM