decodeURI将空格解码成‘+’?
如题
exercise_html.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>练习</title> </head> <body> <form action="exercise2_html.html"> 请输入您要搜索的关键词 <input type="text" name="keyword"> <button type="submit">搜索</button> </form> </body> </html>
exercise2_html.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>练习2</title>
<script>
window.addEventListener('DOMContentLoaded', function() {
let a = window.decodeURI(location.search).substring(1);
let b = a.split('=')[1];
let span = document.getElementsByTagName('span')[0];
span.innerText = b;
})
</script>
</head>
<body>
您搜索的是
<span></span>
</body>
</html>3
收起
正在回答
1回答
同学,你好!是的,在URL中空格会被编码为加号,这是因为URL中不能直接包含空格,所以需要将其进行编码以避免冲突。同学可使用正则将加号匹配出来,使用空格进行替换



祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星