简体   繁体   English

单击标签a时如何向href URL添加参数?

[英]How can add parameters to href url when click on tag a?

I want From home page send parameter id to carts page: 我想从home发送参数idcarts页面:

This is current code, but it not woking: 这是当前代码,但无法正常运行:

<a  href="carts" onclick="addParam();">detail</a>
<script>

     function addParam()
     {
         var url = window.location.href;
         var listId = localStorage.getItem('IDs', '');          
         url=url+ "?id=" +listId;
         window.location.href = url;
     }
</script>

How can add parameters to href url when click on tag a? 单击标签a时如何向href URL添加参数?

Either just override it by grabbing the element's (here done by passing this to the function) href 要么只是通过抓住元素(在这里通过传递完成覆盖它this该功能) href

<a  href="carts" onclick="addParam(this);">detail</a>
<script>

     function addParam(el)
     {
         var listId = localStorage.getItem('IDs', '');          
         window.location.href = el.href + "?id=" +listId;
     }
</script>

Or append it to the element's href 或将其附加到元素的href

<a  href="carts" onclick="addParam(this);">detail</a>
<script>

     function addParam(el)
     {
         var listId = localStorage.getItem('IDs', '');          
         el.href += "?id=" +listId;
     }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 <a>单击指向外部站点的链接时,</a>如何将当前 url 参数添加到 an 的 href - How can I add the current url parameters to the href of an <a> when clicking link to external site 单击“ a href”时如何添加预加载器? - How to add a preloader when you click on a “a href”? 单击href链接时如何将查询字符串参数添加到相同的URL - how to add query string parameter to same URL when on click a href link 如何使用.attr(“href”)前面的参数添加当前网址? - How to add current url with parameters infront of .attr(“href”)? 如何在<a>标签中</a>添加onclick但不执行href =“ url” <a>?</a> - How to add an onclick but don't execute href=“url” in a <a> tag? 如何从javascript的href标记中的json数据添加url - how to add url from json data in the href tag in javascript 在 n 单击时将 href 添加到锚标记 - Add href to anchor tag on the n click 使用JavaScript和特定条件在href网址上添加参数 - Add parameters on href url with javascript and with specific conditions 从<a href>刚刚单击</a>的URL添加参数到URL - Add parameters to an URL from a <a href> just clicked 如何制作<a>标签,以便在单击该</a> <a>标签时,URL不应出现在地址栏中?</a> - how can I make a <a> tag so that when I click that <a> tag,the URL should not go in the address bar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM