简体   繁体   English

使用jQuery替换最后一个逗号?

[英]Replace last comma using jQuery?

http://jsbin.com/uxepap/3/edit http://jsbin.com/uxepap/3/edit

Here is div with links inside. 这是带有内部链接的div。 I'm trying to replace comma inside last <a> with point "." 我试图用点“。”替换最后一个<a>内的逗号。

This comma comes from numeral sort of the list, should be replaced with point. 该逗号来自列表的数字排序,应替换为点。

The problem is, all links stored inside links var, not sure how to get the last one and then replace it. 问题是,存储在links var中的所有链接,不确定如何获取最后一个然后替换它。

Any thoughts? 有什么想法吗?

Use String.replace 使用String.replace

var links = $('a');

var $last = links.filter(":last");

$last.html( $last.html().replace(",", "."));

http://jsbin.com/uxepap/5/edit http://jsbin.com/uxepap/5/edit

links.eq(-1).html(function(index, value) {
  return value.replace(/,\s*$/, ".");
});

DEMO: http://jsbin.com/uxepap/7/edit 演示: http : //jsbin.com/uxepap/7/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM