简体   繁体   English

jQuery:在div标签中包含每两个单词

[英]jQuery: wrap every 2 words in div tag

I'm trying to wrap every 2 words in a <div> tag. 我试图在<div>标签中包装每两个单词。
eg the desired output to look like this: 例如,所需的输出看起来像这样:
<div>one two</div><div>three four</div><div>five six</div> etc etc. <div>one two</div><div>three four</div><div>five six</div>等等
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: 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: 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. 我已经尝试在html中的<h2> <div>标签之后添加一个<div>标签来实现这种效果,但它似乎仍然无法正常工作。 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>" 最后一行是字符串"<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/ http://jsfiddle.net/6JeFH/1/

You explicitly skip the first match in the loop, so adding newhtml.push("<div>") before the loop would help. 您显式跳过循环中的第一个匹配,因此在循环之前添加newhtml.push("<div>")会有所帮助。 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. 这仍然会留下你创建一个未关闭的div的问题,因此建议在循环之后添加newhtml.push("</div>")

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 :) 编辑:其实Entoarox的代码答案:)

You can do this: 你可以这样做:

HTML: (no div in there yet) HTML :(还没有div)

<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): JS(注意最后一行):

$("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 第一个不依赖于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
    </div>
</h2>

And one where there is only the <h2> tag and an odd number of words 还有一个只有<h2>标签和奇数个字的地方

<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 两个都运行相同的JavaScript

$("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(" "));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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