在我制作的脚本中,jquery 从 url 参数中获取变量。它得到的值是一个 url,所以如果它像
http://localhost/index.html?url=http://www.example.com/index.php?something=some
上面写着:
url = http://www.example.com/index.php?something
如果喜欢
http://localhost/index.html?url=http://www.example.com/index.php?something%3Dsome
上面写着:
url = <a href="http://www.example.com/index.php?something%3Dsome" rel="noreferrer noopener nofollow">http://www.example.com/index.php?something%3Dsome</a>
这将注册为有效的 url。我的问题是如何搜索 =
登录 url
变量并将其替换为十六进制 %3D
使用 jquery 还是 javascript?
最佳答案
使用(内置)encodeURIComponent()
函数:
url = 'http://localhost/index.html?url=' +
encodeURIComponent('http://www.example.com/index.php?something=some');
关于javascript - 如何将 url/text 中的符号转换为十六进制字符? (转换 = 到 %3D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6624874/