Javascript 使用 jquery 将参数添加到页面上的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2667551/
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-23 01:23:00  来源:igfitidea点击:

add parameter to links on page using jquery

javascriptjquery

提问by Priyo

How do I add let's say something like ajax=1to all links on my page with jquery. I will also need to check if the url has existing parameters. for example http://example.com/index.php?pl=132will have to become http://example.com/index.php?pl=132&ajax=1

我如何添加让我们ajax=1用 jquery 对我页面上的所有链接说一些类似的话。我还需要检查 url 是否有现有参数。例如http://example.com/index.php?pl=132必须成为http://example.com/index.php?pl=132&ajax=1

Also, if the link does not have any parameters, for example http://example.com/index.php, it will become http://example.com/index.php?ajax=1I want to load the jQuery script on document ready so all links are changed on page load.

此外,如果链接没有任何参数,例如http://example.com/index.phphttp://example.com/index.php?ajax=1我想在文档准备好时加载 jQuery 脚本,以便在页面加载时更改所有链接。

采纳答案by Sampson

If you're interested in plugins, there's the jQuery Query String Object. This will allow you simple checking of parameters in the querystring, and if necessary the ability to add more, remove some, or edit others.

如果您对插件感兴趣,可以使用 jQuery Query String Object。这将允许您简单地检查查询字符串中的参数,并在必要时能够添加更多、删除一些或编辑其他。

回答by Nick Craver

You could do something like this:

你可以这样做:

$(function() {
   $("a").attr('href', function(i, h) {
     return h + (h.indexOf('?') != -1 ? "&ajax=1" : "?ajax=1");
   });
});

On document.readythis looks at every <a>, looks at it's href, if it contains ?already it appends &ajax=1if it doesn't, it appends ?ajax=1.

document.readythis 上查看 each <a>,查看它的 href ,如果它?已经包含则追加,&ajax=1如果没有,则追加?ajax=1

回答by seanhodges

Here is a solution I put together for native Javascript, it supports existing query strings and anchors:

这是我为原生 Javascript 编写的解决方案,它支持现有的查询字符串和锚点:

function addToQueryString(url, key, value) {
    var query = url.indexOf('?');
    var anchor = url.indexOf('#');
    if (query == url.length - 1) {
        // Strip any ? on the end of the URL
        url = url.substring(0, query);
        query = -1;
    }
    return (anchor > 0 ? url.substring(0, anchor) : url)
         + (query > 0 ? "&" + key + "=" + value : "?" + key + "=" + value)
         + (anchor > 0 ? url.substring(anchor) : "");
}

I've posted my existing tests on JSBin: http://jsbin.com/otapem/2/

我已经在 J​​SBin 上发布了我现有的测试:http: //jsbin.com/otapem/2/

回答by SLaks

Like this:

像这样:

$(function() {
    $('a[href]').attr('href', function(index, href) {
        var param = "key=value";

        if (href.charAt(href.length - 1) === '?') //Very unlikely
            return href + param;
        else if (href.indexOf('?') > 0)
            return href + '&' + param;
        else
            return href + '?' + param;
    });
})

回答by Francisco Alvarez

Taking what is above, this is the best way to concatenate parameters to links.

综上所述,这是将参数连接到链接的最佳方式。

Also avoid link with href like href="Javascript:YourFunction();"

还要避免使用 href 链接,例如 href="Javascript:YourFunction();"

$(function(){                                      
   var myRandom = getRandom();       
   $('a[href]').each(function(i){
        var currHref = $(this).attr("href");
        var isAfunction = currHref.substring(0,10);              
        if(isAfunction == "javascript"){                
            $(this).attr("href",currHref);
        }else{
            if (currHref.charAt(currHref.length - 1) === '?')                           
                $(this).attr("href",currHref);
            else if (currHref.indexOf('?') > 0)                
                $(this).attr("href",currHref+"&rand="+getRandom());
            else                  
                $(this).attr("href",currHref+"?rand="+getRandom());
        }
   });               
});
function getRandom(){
    var randomnumber = Math.floor(Math.random()*10000); 
    return randomnumber;
}

回答by Yothin Jetiyanon

a href must be full and corrected URL.

href 必须是完整且更正的 URL。

$(function() {
  $('a[href]').attr('href', function(index, href) {
    var url = new URL(href);
    url.searchParams.set('ajax',1);
    return url;
  });
})

JSFiddle

JSFiddle