我试图用带有html元素的函数包装返回值

时间:2022-11-28 07:25:39

Hi I am using the suffix function to add the th, st and rd to numbers, but I am trying to wrap the actual suffix with a span, somehow it doesn't work, not sure why.

嗨我使用后缀函数将th,st和rd添加到数字,但我试图用span填充实际后缀,不知何故它不起作用,不知道为什么。

Thanks for the help, it worked switch it to html, is there any chance to remove the counter itself, as I am getting all the numbers from the DB, just need to add the suffix to the number, not both. I removed the numbers from the divs and still get the numbers.

谢谢你的帮助,它工作切换到HTML,有没有机会删除计数器本身,因为我从数据库中获取所有数字,只需要添加后缀到数字,而不是两者。我从div中删除了数字,仍然得到了数字。

here is the code

这是代码

<div></div>
<div></div>
<div></div>
<div></div>

$("div").html(function (i, t) {
        i++;
        var str = i.toString().slice(-1),
            suffix = '';
        switch (str) {
            case '1':
                suffix = i.toString().slice(-2) === '11' ? 'th': 'st';
                break;
            case '2':
                suffix = i.toString().slice(-2) === '12' ? 'th' : 'nd';
                break;
            case '3':
                suffix = i.toString().slice(-2) === '13' ? 'th' : 'rd';
                break;
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            case '0':
                suffix = 'th';
                break;
        }
         return i + '<span>' + suffix + '</span>';
    });

1 个解决方案

#1


2  

does it work if you change .text() to .html()?

如果你将.text()更改为.html(),它会起作用吗?

<div>1</div>
<div>2</div>    
<div>3</div>
<div>4</div>

$("div").html(function (i, t) {
    i++;
    var str = i.toString().slice(-1),
        suffix = '';
    switch (str) {
        case '1':
            suffix = i.toString().slice(-2) === '11' ? 'th': 'st';
            break;
        case '2':
            suffix = i.toString().slice(-2) === '12' ? 'th' : 'nd';
            break;
        case '3':
            suffix = i.toString().slice(-2) === '13' ? 'th' : 'rd';
            break;
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case '0':
            suffix = 'th';
            break;
    }
     return i + '<span>' + suffix + '</span>';
});

example: http://jsfiddle.net/2d02k5ou/

#1


2  

does it work if you change .text() to .html()?

如果你将.text()更改为.html(),它会起作用吗?

<div>1</div>
<div>2</div>    
<div>3</div>
<div>4</div>

$("div").html(function (i, t) {
    i++;
    var str = i.toString().slice(-1),
        suffix = '';
    switch (str) {
        case '1':
            suffix = i.toString().slice(-2) === '11' ? 'th': 'st';
            break;
        case '2':
            suffix = i.toString().slice(-2) === '12' ? 'th' : 'nd';
            break;
        case '3':
            suffix = i.toString().slice(-2) === '13' ? 'th' : 'rd';
            break;
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case '0':
            suffix = 'th';
            break;
    }
     return i + '<span>' + suffix + '</span>';
});

example: http://jsfiddle.net/2d02k5ou/