Let's say I have this div:
假设我有这个div:
<div> Let's say these are the words in my div </div>
I know I can wrap every words in the div into a span this way
我知道我可以通过这种方式将div中的每个单词都包含在内
$("div").contents().wrap('<span class="wrapper"/>');
//output: <div><span class="wrapper"> Let's say these are the words in my div</span> </div>
However, I would like to achieve this instead:
但是,我想改为:
<div>
<span class="wrapper">Let's say these</span>
<span class="wrapper">are the words</span>
<span class="wrapper">in my div</span>
</div>
Every particular amount of words (this case: every 3 words) are to be divided into a group and each group of words is to be wrapped separately.
每个特定数量的单词(这种情况:每3个单词)将被分成一组,每组单词将被分开包装。
These are what first come to my mind:
这些是我首先想到的:
1) I think it can be achieved by using text()
to obtain the string and split(' ')
it to form an array with each element contains a word, write a while loop to manipulate the array:
1)我认为可以通过使用text()获取字符串并将其拆分('')来形成一个数组,每个元素包含一个单词,写一个while循环来操作数组:
var a = 0;
var b = array.length;
while (a<b) {
array[a] = "<span class="wrapper>" + array[a]";
a +=2;
if (a>b) {
a = b-1;
}
array[a] = array[a] + "</span>";
a++;
}
then just simply .join('')
the array to form a string and $("div").html(string)
;
然后只需简单地.join('')数组形成一个字符串和$(“div”)。html(string);
2) Or I can simply use regular expression after obtaining with text()
:
2)或者我可以在使用text()获取后简单地使用正则表达式:
do a global search for expressions containing a word + a space + a word + a space + another word
全局搜索包含单词+空格+单词+空格+另一个单词的表达式
/(\w+\s+\w+\s+\w+)/g
replace it with it wrapped in a span
用它包裹在一个跨度中替换它
<span>$1</span>
and html()
the output before performing a $("div").contents().eq(2).wrap('<span class="wrapper"/>')
for the odd one out if there is any.
和html()输出之前执行$(“div”)。contents()。eq(2).wrap('')的奇数输出(如果有的话)。
These are what I've come up with and I want to know, are there better ways other than these?
这些是我想出来的,我想知道,除了这些之外还有更好的方法吗?
And what's the best way (fastest & require least memory) to achieve it?
什么是实现它的最佳方式(最快和需要最少的内存)?
2 个解决方案
#1
3
This ought to do what you want:
这应该做你想要的:
Demo here: http://jsfiddle.net/UQk7r/
在这里演示:http://jsfiddle.net/UQk7r/
$("div").each(function() {
var out = [];
var words = $(this).text()..trim().split(' ');
for (var i = 0; i < words.length; i += 3) {
out.push('<span class="wrapper">' + words.slice(i, i+3).join(' ') + '</span>');
}
$(this).html(out.join(' '));
});
Here's the DOM version (no jQuery):
这是DOM版本(没有jQuery):
Demo here: http://jsfiddle.net/cCege/2/
在这里演示:http://jsfiddle.net/cCege/2/
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
var out = [];
var words = divs[i].innerText.trim().split(' ');
for (var j = 0; j < words.length; j += 3) {
out.push('<span class="wrapper">' + words.slice(j, j+3).join(' ') + '</span>');
}
divs[i].innerHTML = out.join(' ');
}
Finally, here's a DOM + RegEx version... this should be your optimal performer:
最后,这是一个DOM + RegEx版本......这应该是你的最佳表现者:
Demo: http://jsfiddle.net/Xgm5q/1/
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
divs[i].innerHTML = '<span class="wrapper">'
+ divs[i].innerText.trim().replace(/(([^\s]+\s+){2}[^\s]+)\s+/g, '$1</span> <span class="wrapper">')
+ '</span>';
}
#2
3
For performance :
表现:
var elems = document.getElementsByTagName('div');
for (var j=elems.length; j--;) {
var txtArr = elems[j].textContent.replace(/([a-zA-Z]+)\s([a-zA-Z]+)\s([a-zA-Z]+)/g, '$1 $2 $3* ').split('*'),
txtElm = document.createDocumentFragment(),
span = document.createElement('span');
for (i=0; i<txtArr.length; i++) {
if (/\S/.test(txtArr[i])) {
var sp = span.cloneNode(true),
text = document.createTextNode(txtArr[i]);
sp.appendChild(text);
txtElm.appendChild(sp);
}
}
elems[j].innerHTML = '';
elems[j].appendChild(txtElm)
}
#1
3
This ought to do what you want:
这应该做你想要的:
Demo here: http://jsfiddle.net/UQk7r/
在这里演示:http://jsfiddle.net/UQk7r/
$("div").each(function() {
var out = [];
var words = $(this).text()..trim().split(' ');
for (var i = 0; i < words.length; i += 3) {
out.push('<span class="wrapper">' + words.slice(i, i+3).join(' ') + '</span>');
}
$(this).html(out.join(' '));
});
Here's the DOM version (no jQuery):
这是DOM版本(没有jQuery):
Demo here: http://jsfiddle.net/cCege/2/
在这里演示:http://jsfiddle.net/cCege/2/
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
var out = [];
var words = divs[i].innerText.trim().split(' ');
for (var j = 0; j < words.length; j += 3) {
out.push('<span class="wrapper">' + words.slice(j, j+3).join(' ') + '</span>');
}
divs[i].innerHTML = out.join(' ');
}
Finally, here's a DOM + RegEx version... this should be your optimal performer:
最后,这是一个DOM + RegEx版本......这应该是你的最佳表现者:
Demo: http://jsfiddle.net/Xgm5q/1/
var divs = document.getElementsByTagName('div');
for (var i = 0; i < divs.length; i++) {
divs[i].innerHTML = '<span class="wrapper">'
+ divs[i].innerText.trim().replace(/(([^\s]+\s+){2}[^\s]+)\s+/g, '$1</span> <span class="wrapper">')
+ '</span>';
}
#2
3
For performance :
表现:
var elems = document.getElementsByTagName('div');
for (var j=elems.length; j--;) {
var txtArr = elems[j].textContent.replace(/([a-zA-Z]+)\s([a-zA-Z]+)\s([a-zA-Z]+)/g, '$1 $2 $3* ').split('*'),
txtElm = document.createDocumentFragment(),
span = document.createElement('span');
for (i=0; i<txtArr.length; i++) {
if (/\S/.test(txtArr[i])) {
var sp = span.cloneNode(true),
text = document.createTextNode(txtArr[i]);
sp.appendChild(text);
txtElm.appendChild(sp);
}
}
elems[j].innerHTML = '';
elems[j].appendChild(txtElm)
}