id.substr(id.length - 1); //get the last characterid.substr(2); //get the characters from the 3rd character onid.substr(2, 1); //get the 3rd characterid.substr(2, 2); //get the 3rd and 4th characters
$('.rating').on('rating.change', function() {alert($(this).val());// console.log(this.id);var static_id_text=("rating_element-").length;var product_id = this.id.slice(static_id_text); //get the length in order to deduct from the whole stringconsole.log(product_id );//outputs the last id appended});
var string = "hello";var last = string.substr(-1);
非常灵活。例如:
// Get 2 characters, 1 character from end// The first part says how many characters// to go back and the second says how many// to go forward. If you don't say how many// to go forward it will include everythingvar string = "hello!";var lasttwo = string.substr(-3,2);// = "lo"