jQuery 如何在jquery中将变量附加到url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5452158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:11:37 来源:igfitidea点击:
How to append variable to url in jquery?
提问by Steffi
How can I append variable/text
to the attribute href
in jquery ?
如何附加variable/text
到href
jquery 中的属性?
回答by Darin Dimitrov
回答by JohnC
You need to check if there is already a query string before appending items to the url based on the other examples you'd do something like.
您需要根据您要执行的其他示例将项目附加到 url 之前检查是否已经存在查询字符串。
var foo = 'foo=bar';
$('a').attr('href', function(index, attr) {
return attr + (attr.indexOf('?') >=0 ? '&' : '?') + foo;
});
回答by ?ime Vidas
$('a').attr('href', function(i, v) {
return v + 'text...';
});
回答by zoombaro
If you use it repeatedly try .attr() as:
如果您反复使用它,请尝试 .attr() 为:
HTML: <a id='foo' href='#'>I am your father,.. nooooo</a>
var myparams = 'myvar=myvalue';
var myurl = 'TypeThe_NetscapeURL';
$('a#foo').attr('href', function() {
return myurl + '?' + myparams;
});