使用 JS 中的 encodeURIComponent 或 encodeURI 方法。
encodeURIComponent(String)
对传递参数进行设置。不编码字符有 71 个: ! , ' , ( , ) , * , - , . , _ , ~ , 0-9 , a-z , A-Z 。
例:
var url = “<a href=’http://cancait.blog.163.com/name=” + encodeURIComponent(“ 中国 ”) + “’> 中国 </a>”;
encodeURI(String)
对 URL 整体转换。不编码字符有 82 个: ! , # , $ , & , ' , ( , ) , * , + ,,, - , . , / , : , ; , = , ? , @ , _ , ~ , 0-9 , a-z , A-Z 。
例:
var url = “<a href=’” + encodeURI(“http://cancait.blog.163.com/name= 中国 ”) + “’> 中国 </a>”;
本文介绍了JavaScript中用于处理URL编码的两种方法:encodeURIComponent与encodeURI。这两种方法分别用于对字符串进行特定的编码操作,以确保它们能正确地作为URL的一部分被传输。文章通过实例展示了如何使用这些方法。
654

被折叠的 条评论
为什么被折叠?



