简体   繁体   中英

jQuery: wrap every 2 words in div tag

I'm trying to wrap every 2 words in a <div> tag.
eg the desired output to look like this:
<div>one two</div><div>three four</div><div>five six</div> etc etc.
But what I'm actually getting, the first two words are being ignored, and I'm actually getting:
one two<div>three four</div><div>five six</div>
Here's my code,
HTML:

<h2><div>This is some long heading text to use as an example. This is some long heading text to use as an example</h2>

jQuery:

$("h2 div").each(function() {
    var html = $(this).html().split(" ");
    var newhtml = [];

    for(var i=0; i< html.length; i++) {

        if(i>0 && (i%2) == 0)
            newhtml.push("</div><div>");

        newhtml.push(html[i]);
    }

    $(this).html(newhtml.join(" "));
});

I've tried adding a <div> tag after the <h2> tag in the html to achieve this effect, but it still doesn't seem to be working. Any suggestions would be greatly appreciated!

var html = 'This is some long heading text to use as an example.'.split(" ");
var newhtml = [];
for( var i=0 ; i<html.length-1 ; i+=2 ) newhtml.push(html[i]+' '+html[i+1]);
if( html.length&1 ) newhtml.push(html[html.length-1]);

'<div>'+newhtml.join('</div><div>')+'</div>'

The final line is the string "<div>This is</div><div>some long</div><div>heading text</div><div>to use</div><div>as an</div><div>example.</div>"

I'm not a regex pro by any means, but just to offer a possible alternative, I think you could do this using something like:

string.replace(/(\S+ \S+)/g, "<div>$1</div>");

Like so:

$("h2 div").each(function(){
    this.innerHTML = this.innerHTML.replace(/(\S+ \S+)/g, "<div>$1</div>");
});

http://jsfiddle.net/6JeFH/1/

You explicitly skip the first match in the loop, so adding newhtml.push("<div>") before the loop would help. This still leaves the issue of you creating a div that is not closed, so adding newhtml.push("</div>") after the loop would be recommended aswell.

This is working for me:

<h2>This is some long heading text to use as an example. This is some long heading text to use as an example</h2>
<script type="text/javascript">
    $("h2").each(function() {
        var html = $(this).html().split(" ");
        var newhtml = [];
        newhtml.push("<div>");

        for(var i=0; i< html.length; i++) {

            if(i>0 && (i%2) == 0)
                newhtml.push("</div><div>");

            newhtml.push(html[i]);
        }

        newhtml.push("</div>");

        $(this).html(newhtml.join(" "));
    });
</script>

Edit: Actually the code for Entoarox answer :)

You can do this:

HTML: (no div in there yet)

<h2>This is some long heading text to use as an example. This is some long heading text to use as an example</h2>

JS (notice the last line):

$("h2").each(function() {
    var html = $(this).html().split(" ");
    var newhtml = [];

    for(var i=0; i< html.length; i++) {

        if(i>0 && (i%2) == 0)
            newhtml.push("</div><div>");

        newhtml.push(html[i]);
    }

    $(this).html("<div>"+ newhtml.join(" ") +"</div>");
});

Please have a look at the fiddle:

I have provided two examples

The first does not rely on the html being initially malformed

<h2>
     <div>
          This is some long heading text to use as an example. 
          This is some long heading text to use as an example
    </div>
</h2>

And one where there is only the <h2> tag and an odd number of words

<h2>
    This is some long heading text to use as an example. 
    This is some long heading text to use as an example oddnumber
</h2>

The same JavaScript runs on both of them

$("h2 > div").each(divByTwo);
$("h2").each(divByTwo);

function divByTwo() {
    var html = $(this).html().trim().split(" ");
    var newhtml = [];   
    for(var i=1; i< html.length; i += 2) {
        newhtml.push("<div>" + html[i - 1] + " " + html[i]+ "</div>");       
    }

    if (html.length % 2 !== 0) {
        newhtml.push("<div>" + html[html.length - 1] + "</div>");
    }
    $(this).html(newhtml.join(" "));
}

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