我正在尝试对字符串做两件事:
- 删除第一个字符($ 符号)。
- 将小数位及其后面的数字 (.00) 放在“分”类的范围内。
这是我的代码,但是当它在 jQuery 1.6.4 上运行时,客户端正在运行 jQuery 1.3.2 我该如何解决这个问题?
非常感谢。
<span class="price">$90.00</span>
<script type="text/javascript">
$(".price").html(function (i, html) {
return html.replace(/(\.\d+)$/,'<span class="cents">$1</span>').slice(1)
});
</script>
编辑: fiddle :http://goo.gl/79EvZw
最佳答案
jQuery 中接受函数的 html()
方法是在 1.4 版本中添加的。
在版本 1.3 中,您只能将 HTML 字符串传递到此函数中。
V1.0至V1.3
.html( htmlString )
v1.4
.html( htmlString )
.html( function(index, oldhtml) )
要在 jQuery 1.3 中获得相同的行为,您可以执行以下操作:
$(".price").each(function(index, elem) {
var price = $(elem);
var oldHtml = price.html();
var newHtml = oldHtml.replace(/(\.\d+)$/,'<span class="cents">$1</span>').slice(1);
price.html(newHtml);
});
关于javascript - 字符串修改在 jQuery 1.6.4 中有效,但在 1.3.2 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367638/