javascript 用于 href 的 jQuery encodeURI 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3660062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:51:28  来源:igfitidea点击:

jQuery encodeURI for href not working

javascriptjqueryurlencodeencodeuricomponent

提问by holden

I'm having problems encoding a string so I can place a variable into a link. I'm sure this is really simple, but i had trouble turning anything up.

我在编码字符串时遇到问题,因此我可以将变量放入链接中。我相信这真的很简单,但我无法打开任何东西。

$("a.inquiry").attr("href", "/inquiry/6933/text=" + encodeURI("text o"));

This doesn't work.

这不起作用。

encodeURI("text o")

Still returns:

仍然返回:

link/text o

Instead of:

代替:

link/text%20

Also tried:

还试过:

$("a.inquiry").attr("href", encodeURIComponent("/inquiry/6933/text=" + "text o"));

回答by Sarfraz

Try with encodeURIComponentinstead.

试试吧encodeURIComponent

回答by Stevie B

I thought I was having the same problem, but then I realized I was looking at the results in both the mouseOver of the resulting link and in the address bar after clicking the link. It turns out that my browser was showing the spaces in both cases - i.e. when I copied the URI out of the address bar and pasted it in emacs the %20 revealed itself.

我以为我遇到了同样的问题,但后来我意识到我正在查看结果链接的 mouseOver 和单击链接后地址栏中的结果。事实证明,我的浏览器在这两种情况下都显示了空格 - 即当我从地址栏中复制 URI 并将其粘贴到 emacs 中时,%20 显示出来了。