检查href是否存在jQuery中的查询字符串 [英] Checking href for presence of a query string in jQuery

查看:155
本文介绍了检查href是否存在jQuery中的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一些jQuery用于附加带有一些位置信息的URL。

I currently have this bit of jQuery I am using to append a URL with some location information.

jQuery('a').attr('href', function() {
            return this.href + "&location=/123/abc";
       });

我的问题是大多数链接都有?其中使用上述&好。有一些选择不。我想检查网址,看看是否有?如果有我想使用& location = / 123 / abc,如果没有?我将需要使用?location = / 123 / abc

My issue is most links have a ? in which makes for the use of the above & ok. There are a select few that dont. I am looking to check the url to see if there is a ?. If there is I want to use "&location=/123/abc", if there is no ? I will need to use "?location=/123/abc"

我不是if / else语句的最佳选择。任何帮助将不胜感激。

I am not the best with if/else statements. Any help would be appreciated.

if (thereIsA?InTheUrl) {

    return this.href + "&location=/123/abc";

} else {

    return this.href + "?location=/123/abc";

}

类似的东西,只是不确定哦写它。

Something like that, just not sure oh to write it.

推荐答案

   jQuery('a').attr('href', function() {
        return (this.href.indexOf("?") >= 0) ? this.href + "&location=/123/abc" : this.href + "?location=/123/abc";
   });

这篇关于检查href是否存在jQuery中的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆