javascript - url编码
类似的业务场景:http://www.baidu.com?name=3&age=4
你想表达的意思,其实是:name的值是 “3&age=4”。
这时候要用到 encodeURIComponent(),他会把路径中的特殊字符进行编码、转义。
在Java中也有类似的语法,用来解决中文路径、空格等问题。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
asdad
<a id="test" href="#">asdasd</a>
<script type="text/javascript">
var ur = encodeURIComponent('a=http://www.baidu.com?a=这是一段中文&b=3');
console.log(ur)
document.getElementById('test').setAttribute('href', 'http://localhost:8080/Sea/test?' + ur);
</script>
</body>
</html>
疯狂的妞妞 :每一天,做什么都好,不要什么都不做!