简体   繁体   English

将参数传递给具有锚点的链接

[英]Pass Parameters To Links With An Anchor

I am trying to pass url parameters through to the next pages by appending them to all the links on the page. 我正在尝试通过将url参数附加到页面上的所有链接来将其传递给下一页。 The code I have works fine for this purpose. 为此,我拥有的代码工作正常。

I am encountering trouble when having anchor links on the page, as it puts the parameters after the URL and after the anchor as well, preventing the link from going to the anchor point. 我在页面上具有锚链接时遇到麻烦,因为它将参数放在URL之后以及锚之后,从而阻止了链接到达锚点。

Ex. 例如 website.com/?param1=test/#anchor?param1=test website.com/?param1=测试/#a​​nchor?param1 =测试

Here is my code: 这是我的代码:

<script type="text/javascript">

jQuery(document).ready( function($) {

var params = window.location.search.replace(/\+/g,'%20'); 
var button = $('a[href]').each( function(i) { 
  this.href = this.href + params; 
}); 

});

</script>

Any help would be greatly appreciated! 任何帮助将不胜感激!

There you go: 你去了:

var button = $('a[href]').each( function(i) { 
  var clearUrl = this.href.split("#");
  clearUrl[0] += params;
  this.href = clearUrl.join("#");
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM