decodeURI将空格解码成‘+’?

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>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学,你好!是的,在URL中空格会被编码为加号,这是因为URL中不能直接包含空格,所以需要将其进行编码以避免冲突。同学可使用正则将加号匹配出来,使用空格进行替换

https://img1.sycdn.imooc.com//climg/64cb095f09a5fcd206050163.jpg

https://img1.sycdn.imooc.com//climg/64cb098909fc628504420055.jpg

https://img1.sycdn.imooc.com//climg/64cb09b40981b1df02180039.jpg

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师