使用 JavaScript 的动态链接
我的网站 (www.mysite.com) 中有以下 html 代码:
<a href="www.website.com?rh=1234">Link 1</a>
<a href="www.website.com/page/?rh=1234">Link 2</a>
我想做的是,当我的网站地址示例 www.mysite.com?r=9876 附加查询字符串“r”时,html 应该更改为:
<a href="www.website.com?r=9876">Link 1</a>
<a href="www.website.com/page/?r=9876">Link 2</a>
我想这可以用 javascript 实现,但我不知道该怎么做。我发现这个< /a> 在我的研究过程中,但这并不完全是我正在寻找的。我希望仅当存在查询字符串时才发生更改。有人可以帮助我吗?
I have the following html code in my website (www.mysite.com) :
<a href="www.website.com?rh=1234">Link 1</a>
<a href="www.website.com/page/?rh=1234">Link 2</a>
What I'm trying to do is, when there is a query string "r" attached to my website's address example www.mysite.com?r=9876, the html should change to:
<a href="www.website.com?r=9876">Link 1</a>
<a href="www.website.com/page/?r=9876">Link 2</a>
I guess this is possible with javascript, but I'm not sure how to do it. I found this during my research, but its not exactly what I'm looking for. I want the change to happen only when there is a query string. Can someone please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该脚本将执行此操作:
This script will do it:
使用 jquery 选择器选择 href 属性中包含 rh 的链接
http://api.jquery.com/attribute-contains-selector/
然后使用javascript替换功能
http://www.w3schools.com/jsref/jsref_replace.asp 替换 ' rh' 与 'r'。
代码是这个
$('a[href*="www.website.com"]').attr("href",function(i,val){
return val.replace("rh","r");
});
你可以在 http://jsfiddle.net/8jCpg/2/
代码正在做我上面所说的事情。我希望它对你有帮助。
Use jquery selectors to select links that have rh in their href attribute
http://api.jquery.com/attribute-contains-selector/
Then use javascript replace function
http://www.w3schools.com/jsref/jsref_replace.asp to replace the 'rh' with 'r'.
Code is this
$('a[href*="www.website.com"]').attr("href",function(i,val){
return val.replace("rh","r");
});
you can look demo at http://jsfiddle.net/8jCpg/2/
Code is doing the thing which i exactly said above. I hope it help you.