包装跨度中的每个字符 [英] Wrapping each character in a span

查看:97
本文介绍了包装跨度中的每个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将每个数字包装在一个范围内.

I'm trying to wrap each number in a span.

这是我的位置.

<div class="numbers">12345</div>
<div class="numbers">12345</div>
<div class="numbers">12345</div>

$('.numbers').each(function (index) {
    var characters = $(this).text().split("");
    $(this).empty();

    $.each(characters, function (i, el) {
    $(this).append("<span>" + el + "</span");
    });

});

我的语法哪里出问题了?

Where could I be going wrong with the syntax?

推荐答案

this$('.numbers').each中的this中是不同的,因此您必须将其保存到一个变量中在那里使用.

this in the $.each is not the same in the this in $('.numbers').each so you'll have to save it to a variable to use it there.

$this = $(this);
$this.empty();
$.each(characters, function (i, el) {
    $this.append("<span>" + el + "</span");
});

http://jsfiddle.net/phA8q/

这篇关于包装跨度中的每个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆