jQuery用跨度包装数字[重复]

时间:2021-07-03 09:08:02

Possible Duplicate:
jQuery: Wrap each symbol inside tag

可能重复:jQuery:在标记内包装每个符号

I have the following HTML:

我有以下HTML:

<div class="reels">
    £1234
</div>

Which I want to convert to:

我要转换为:

<div class="reels">
    £<span>1</span><span>2</span><span>3</span><span>4</span>
</div>

So basically wrapping a span around each individual number (but not the £ sign) in the 'reels' div. Here's what I have so far:

所以基本上在'reels'div中围绕每个单独的数字(但不是£符号)包裹一个跨度。这是我到目前为止所拥有的:

$('.reels').wrap('<span>');

But this wraps the div with a span rather than the contents of it. If anybody could help, it would be much appreciated!

但是这会用div而不是内容来包装div。如果有人可以提供帮助,我们将不胜感激!

2 个解决方案

#1


8  

$('.reels').html(function(i, v) {
    return v.replace(/(\d)/g, '<span>$1</span>');
});

Here's the fiddle: http://jsfiddle.net/CA5UL/

这是小提琴:http://jsfiddle.net/CA5UL/


Works with multiple .reels elements as well: http://jsfiddle.net/CA5UL/1/

也适用于多个.reels元素:http://jsfiddle.net/CA5UL/1/

#2


3  

A simple RegExp should do the trick:

一个简单的RegExp应该可以做到这一点:

$('.reels').html($('.reels').html().replace(/(\d)/g, '<span>$1</span>'))

See demo

#1


8  

$('.reels').html(function(i, v) {
    return v.replace(/(\d)/g, '<span>$1</span>');
});

Here's the fiddle: http://jsfiddle.net/CA5UL/

这是小提琴:http://jsfiddle.net/CA5UL/


Works with multiple .reels elements as well: http://jsfiddle.net/CA5UL/1/

也适用于多个.reels元素:http://jsfiddle.net/CA5UL/1/

#2


3  

A simple RegExp should do the trick:

一个简单的RegExp应该可以做到这一点:

$('.reels').html($('.reels').html().replace(/(\d)/g, '<span>$1</span>'))

See demo