首页 > 解决方案 > 如何在 Javascript 上正确获取我的 url 参数?

问题描述

我正在尝试将 giphy url 解析为 javascript 参数,它会删除我所有的 //.

这是我的js:

var $save = $("<input type=\"button\" class=\"btn btn-sm btn-success\" value=\"Save\" onclick=\"SaveGif(\"" + gif.fixed_height.url +"\")\" />");

网址是:

https://media1.giphy.com/media/tJqyalvo9ahykfykAj/200.gif?cid=487fb615lw0p7lcstn5iqghh0mxvk4n889nope6ven1n897i&rid=200.gif

这就是 Chrome 解析的内容:

<input type="button" class="btn btn-sm btn-success" value="Save" onclick="SaveGif(" https:="" media1.giphy.com="" media="" tjqyalvo9ahykfykaj="" 200.gif?cid="487fb615lw0p7lcstn5iqghh0mxvk4n889nope6ven1n897i&amp;rid=200.gif&quot;)&quot;">

此外,如果我删除引号,我会得到正确的 url,但没有“”:

<input type="button" class="btn btn-sm btn-success" value="Save" onclick="SaveGif(https://media1.giphy.com/media/tJqyalvo9ahykfykAj/200.gif?cid=487fb615lw0p7lcstn5iqghh0mxvk4n889nope6ven1n897i&amp;rid=200.gif)">

我做错了什么?

谢谢!

标签: javascript

解决方案


使用单引号

var $save = $("<input type=\"button\" class=\"btn btn-sm btn-success\" value=\"Save\" onclick=\"SaveGif('" + gif.fixed_height.url + "')\" />");

推荐阅读