[英]I am trying to wrap a return value from a function with an html element
嗨,我正在使用后缀功能将th,st和rd添加到数字中,但是我试图用一个跨度来包装实际的后缀,但由于某种原因它不起作用,不确定为什么。
感谢您的帮助,它可以将其切换为html,是否有机会删除计数器本身,因为我从数据库中获取了所有数字,只需要在数字中添加后缀即可,而不是两个都添加。 我从div中删除了数字,但仍然得到了数字。
这是代码
<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>';
});
如果将.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>';
});
例如: http : //jsfiddle.net/2d02k5ou/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.