Mic*_*ael 5 javascript string jquery
我目前有一些jQuery,我用来附加一些包含一些位置信息的URL.
jQuery('a').attr('href', function() {
return this.href + "&location=/123/abc";
});
Run Code Online (Sandbox Code Playgroud)
我的问题是大多数链接都有?其中使用上述&ok.有一些选择不.我想检查网址,看看是否有?如果有我想使用"&location =/123/abc",如果没有?我需要使用"?location =/123/abc"
if/else语句我不是最好的.任何帮助,将不胜感激.
if (thereIsA?InTheUrl) {
return this.href + "&location=/123/abc";
} else {
return this.href + "?location=/123/abc";
}
Run Code Online (Sandbox Code Playgroud)
这样的东西,只是不确定哦写它.
jQuery('a').attr('href', function() {
return (this.href.indexOf("?") >= 0) ? this.href + "&location=/123/abc" : this.href + "?location=/123/abc";
});
Run Code Online (Sandbox Code Playgroud)