0

当用户在标签中写东西时,我尝试创建链接。但问题是我无法转换字符集。像 ş = s, ğ = g ...这是我的代码

$(document).ready(function () {
   $('em').html(function(i, linkle) {

var returnString = linkle.toLowerCase();
//Convert Characters
returnString = returnString.replace(/ö/g, 'o');
returnString = returnString.replace(/ç/g, 'c');
returnString = returnString.replace(/ş/g, 's');
returnString = returnString.replace(/ı/g, 'i');
returnString = returnString.replace(/ğ/g, 'g');
returnString = returnString.replace(/ü/g, 'u');  

// if there are other invalid chars, convert them into blank spaces
returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
// convert multiple spaces and hyphens into one space       
returnString = returnString.replace(/[\s-]+/g, " ");
// trims current string
returnString = returnString.replace(/^\s+|\s+$/g,"");
// cuts string (if too long)
if(returnString.length > maxLength)
returnString = returnString.substring(0,maxLength);
// add hyphens
returnString = returnString.replace(/\s/g, "-");    

return '<a href="/' + linkle + '/">' + linkle + '</a>';
});
});

如何转换字符并建立我的链接。最后我想要的是如果第一个和最后一个字符是空白的,清除它...... Ty。

4

1 回答 1

0

jqueryhtml()函数需要一个字符串,但您提供的是一个函数,您应该将该函数移出html调用并传递其返回值。此外,您在构建链接时实际上并没有使用该returnString变量,我认为 return 语句应该是:

return '<a href="/' + returnString + '/">' + returnString + '</a>';

您也可以考虑只使用encodeURI而不是手动替换字符 - 这将转换无效字符,以便在 url 中安全使用。

于 2012-09-10T08:43:45.310 回答