用来自url string javascript的变量替换anchor tag href

时间:2016-02-03 11:47:20

标签: javascript

我有以下java脚本代码:

var product = getQueryVariable("product");

$('a.link').each(function()
{
    $(this).attr("href", $(this).attr("href") + "&product=" + getQueryVariable("product"));
});

function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);        

    var variables = query.split("&");

    for (var iCount = 0; iCount < variables.length; iCount++)
    {
        var pair = variables[iCount].split("=");

        if (pair[0] == variable)
            return pair[1];
    }
}

如果网址为:http://www.xxxx.com/index.htm?product=abc,则设置指向锚标记的链接

<a class="link" href="http://www.aaaa.com/index.htm">Click here</a>

作为

<a class="link" href="http://www.aaaa.com/index.htm?product=abc">Click here</a>

这就是我想要的,它做得很好。

但是,如果锚标记在其product中已经有href个变量,那么

<a class="link" href="http://www.aaaa.com/index.htm?product=xyz">Click here</a>

然后我得到

<a class="link" href="http://www.aaaa.com/index.htm?product=xyz&product=abc">Click here</a>

虽然我期望如果product锚标记的href中存在href变量,它将替换现有的产品变量。如果product标记中没有产品变量,则会附加它。

代码附加良好,但不会替换锚标记href中的现有'product'变量。

所以,问题是如何从包含a类的所有'link'代码中获取$('a.link').each(function()参数?所以如果找到我可以在我的代码中替换它。我可以在UNION ALL中获取它吗?

我该怎么做?

5 个答案:

答案 0 :(得分:0)

您可以这样做:

function changeLinkQueries(link,parameter,replacedVal) {

  // This function is anonymous, is executed immediately and 
  // the return value is assigned to QueryString!
  var query_string = {};
  var query = link.split('?')[1];
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
     var pair = vars[i].split("=");
     // If first entry with this name
     if (typeof query_string[pair[0]] === "undefined") {
       query_string[pair[0]] = decodeURIComponent(pair[1]);
       // If second entry with this name
     } else if (typeof query_string[pair[0]] === "string") {
       var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
       query_string[pair[0]] = arr;
       // If third or later entry with this name
     } else {
       query_string[pair[0]].push(decodeURIComponent(pair[1]));
     }
  } 

  var queries = query_string,
    linkArr = [];
  queries[parameter] = replacedVal;

  for(query in queries){
    linkArr.push(query + '=' + queries[query]);
  }

  return link.split('?')[0] + '?' + linkArr.join('&');

};

功能用法是:changeLinkQueries(link,parameter,replacedVal)

  • link:是您要修改的链接
  • 参数:您要更改的参数
  • replacementVal:您要用它替换的值

示例: changeLinkQueries('http://www.aaaa.com/index.htm?product=xyz','product','test')返回==&gt; http://www.aaaa.com/index.htm?product=test

将此功能添加到您的代码中并将代码更新为:

var product = getQueryVariable("product");

$('a.link').each(function(){
    $(this).attr("href", changeLinkQueries($(this).attr("href"),'product',getQueryVariable('product')));
});

function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);        

    var variables = query.split("&");

    for (var iCount = 0; iCount < variables.length; iCount++)
    {
        var pair = variables[iCount].split("=");

        if (pair[0] == variable)
            return pair[1];
    }
}

答案 1 :(得分:0)

试试这个,

Array.prototype.contains = function(x){
  return this.indexOf( x ) > -1 ? true : false;
}

var getUrlVars = function(url){
    var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(decodeURIComponent(hash[0]));
        vars[decodeURIComponent(hash[0])] = decodeURIComponent(hash[1]);
     }
     if(vars[0] == url){
        vars =[];
     }
     return vars;
}


var getUrl = function(url){
    var urlParams = getUrlVars(window.location.href),
        linkParams = getUrlVars(url),
        altered = false;

    for(i = 0; i < linkParams.length; i++){
        var t = linkParams[i];
        if(urlParams.contains(t)){
            linkParams[t] = urlParams[t];
            altered = true;
        }
    }
    return altered ? url.split("?")[0] + "?" + linkParams.map(function(y){return y + "=" + linkParams[y]}).join("&") : url;
}

$("a.link").each(function (i,link) {
    link.attr("href",getUrl(link.attr("href")));
})

答案 2 :(得分:0)

试试这个

double dRmSP = -1.0;                       //-1.0000   ok
uint16 tSP = static_cast<uint16>(dRmSP);   // = 65535   ok

// retour
double dRmSP = static_cast<double>(tSP);  // = 65535.0000  - as described above

while (dRmSP > 65435.0) dRmSP -= 65536.0;   //  voila!  -1.0 obtained

答案 3 :(得分:0)

此代码有效:

 Uri wholeView = new Uri("ModuleA", UriKind.Relative);                                
 regionManager.RequestNavigate(RegionNames.TheBottomRegion, wholeView);
 var currentView = regionManager.Regions[RegionNames.TheWholeRegion].Views.ElementAt(0);
 regionManager.Regions[RegionNames.TheWholeRegion].Remove(currentView);

答案 4 :(得分:-1)

我希望这会有所帮助,

$(this).attr("href", getQueryVariable($(this).attr("href"), "product"));

function getQueryVariable(hrefLink, variable)
{
    if(hrefLink.contains("product")){
        return hrefLink;
    }else{
        //write your code here and return full url

    }
}