简体   繁体   English

如何在 Javascript 上正确获取我的 url 参数?

[英]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&amp;rid=200.gif&quot;)&quot;">

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&amp;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.

相关问题 提交GET表单后,如何在结果url参数字符串中插入变量? - How can I insert a variable into the resulting url parameter string after submitting my GET form? WordPress:如何将url GET参数添加到主菜单项 - Wordpress: How can I add url GET parameter to my main menu items 如何使用 Javascript 将查询参数 append 作为我的 URL 的查询参数? - How do I append a query parameter to my URL using Javascript? 如何在javascript中获取以前的网址? - How can I get the previous url in javascript? 如何在JavaScript中正确获取这些DOM元素名称? - How do I get these DOM element names right in my javascript? 我如何获取此javascript来调用我的C#方法并传递参数 - How can I get this javascript to call my C# method and pass a Parameter 如何将 URL 参数添加到我网站上的链接 - how can I add a URL parameter to a link on my website 如何在不重新加载页面的情况下向我的 url 添加参数? - How can I add a parameter to my url, without reload the page? 如何获得javascript文件的正确位置? - How can I get the right location for a javascript file? 我如何获取URL中的电子邮件地址,以使用javascript或HTML以及基于Ruby on Rails构建的网站出现在我的网页上? - How can I get the email address in a URL to appear on my webpage with javascript or HTML with a website built on Ruby on Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM