[英]How can i get my url parameter right on Javascript?
i'm trying to parse a giphy url into a javascript parameter and it removes all my //.我正在尝试将 giphy url 解析为 javascript 参数,它删除了我所有的 //.
This is my js:这是我的js:
var $save = $("<input type=\"button\" class=\"btn btn-sm btn-success\" value=\"Save\" onclick=\"SaveGif(\"" + gif.fixed_height.url +"\")\" />");
the url is:网址是:
https://media1.giphy.com/media/tJqyalvo9ahykfykAj/200.gif?cid=487fb615lw0p7lcstn5iqghh0mxvk4n889nope6ven1n897i&rid=200.gif
https://media1.giphy.com/media/tJqyalvo9ahykfykAj/200.gif?cid=487fb615lw0p7lcstn5iqghh0mxvk4n889nope6ven1n897i&rid=200.gif
and this is what Chrome parses:这就是 Chrome 解析的内容:
<input type="button" class="btn btn-sm btn-success" value="Save" onclick="SaveGif(" https:="" media1.giphy.com="" media="" tjqyalvo9ahykfykaj="" 200.gif?cid="487fb615lw0p7lcstn5iqghh0mxvk4n889nope6ven1n897i&rid=200.gif")"">
Also if i remove the quotes i get the correct url but without "":此外,如果我删除引号,我会得到正确的网址,但没有“”:
<input type="button" class="btn btn-sm btn-success" value="Save" onclick="SaveGif(https://media1.giphy.com/media/tJqyalvo9ahykfykAj/200.gif?cid=487fb615lw0p7lcstn5iqghh0mxvk4n889nope6ven1n897i&rid=200.gif)">
what i'm doing wrong?我做错了什么?
thanks!谢谢!
使用单引号
var $save = $("<input type=\"button\" class=\"btn btn-sm btn-success\" value=\"Save\" onclick=\"SaveGif('" + gif.fixed_height.url + "')\" />");
将所有内容包裹在 ` 反引号中 它允许您自由使用 " " 所以它会是这样的
var $save = $(`<input type="button" class="btn btn-sm btn-success" value="Save" onclick=SaveGif( + gif.fixed_height.url +/>`);
If you open and close all that html string with single quotes you don't need any of the attribute double quotes escaped which makes it much easier to both read and write如果您使用单引号打开和关闭所有 html 字符串,则不需要转义任何属性双引号,这使得读取和写入变得更加容易
Then rather than messing with inline onclick
just add a jQuery event listener that calls your function with the appropriate variable passed in然后,而不是搞乱内联
onclick
只需添加一个 jQuery 事件侦听器,该侦听器使用传入的适当变量调用您的函数
var $save = $('<input type="button" class="btn btn-sm btn-success" value="Save" />'); $save.on('click', function(evt) { SaveGif(gif.fixed_height.url) }); // demo code only below const gif = {fixed_height: {url: 'https://media1.giphy.com/media/tJqyalvo9ahykfykAj/200.gif?cid=487fb615lw0p7lcstn5iqghh0mxvk4n889nope6ven1n897i&rid=200.gif'}}; $('body').append($save) function SaveGif(url) { console.log(url) }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.