简体   繁体   中英

Replace last comma using jQuery?

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

Here is div with links inside. I'm trying to replace comma inside last <a> with point "."

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.

Any thoughts?

Use String.replace

var links = $('a');

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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