href 的 jQuery 编码 URI 不起作用
我在编码字符串时遇到问题,因此我可以将变量放入链接中。我确信这真的很简单,但我在打开任何东西时遇到了困难。
$("a.inquiry").attr("href", "/inquiry/6933/text=" + encodeURI("text o"));
这是行不通的。
encodeURI("text o")
仍然返回:
link/text o
而不是:
link/text%20
还尝试过:
$("a.inquiry").attr("href", encodeURIComponent("/inquiry/6933/text=" + "text o"));
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"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
encodeURIComponent
代替。Try with
encodeURIComponent
instead.我以为我遇到了同样的问题,但后来我意识到我正在查看结果链接的鼠标悬停和单击链接后地址栏中的结果。事实证明,我的浏览器在这两种情况下都显示了空格 - 即,当我从地址栏中复制 URI 并将其粘贴到 emacs 中时,%20 就显示出来了。
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.