我已經使用了應答here作爲例子stringToSlug()函數原型,雖然我更願意把它寫這樣的:value.stringToSlug()
JavaScript創建
所以我把它改成這樣:
// String to slug
String.prototype.stringToSlug = function(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
};
它的工作原理,如果我通過這樣的字符串:
var value = $(this).val();
value.stringToSlug(value);
是否有另一種方式來獲得字符串的值,而不把它當作一個PARAM? –