2016-01-22 154 views
-2

我在頁面上有以下鏈接,但是當頁面通過javascript加載時,我想爲它們添加一些url varnames。通過javascript將url鏈接添加到href鏈接

鏈接:

<a href="http://www.test.com/home/">Home</a> 
<a href="http://www.test.com/business/">Business</a> 
<a href="http://www.test.com/commercial/">Commercial</a> 

想將其更改爲:

<a href="http://www.test.com/home?city=miami&tel=7777777777">Home</a> 
<a href="http://www.test.com/business?city=miami&tel=7777777777">Business</a> 
<a href="http://www.test.com/commercial?city=miami&tel=7777777777">Commercial</a> 

我沒有任何的HREF鏈接的ID,所以我無法弄清楚如何找到他們在html中。有人可以幫助我這個。

謝謝

更新 - 段從HTML

<div class="menu_wrapper"> 
<nav id="menu" class="menu-960px-grid-container"><ul id="menu-960px-grid" class="menu"><li id="menu-item-3064" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-67 current_page_item"><a href="http://www.test.com/Home/"><span>Home</span></a></li> 
<li id="menu-item-3042" class="menu-item menu-item-type-post_type menu-item-object-page"><a href="http://www.test.com/business/"><span>Business</span></a></li> 
<li id="menu-item-3043" class="menu-item menu-item-type-post_type menu-item-object-page"><a href="http://www.test.com/commercial/"><span>Commercial</span></a></li> 
</ul></nav><a class="responsive-menu-toggle " href="#"><i class="icon-menu"></i></a>      
       </div> 
+0

這是網頁上的所有鏈接嗎?有沒有辦法選擇你需要的?他們住在菜單中嗎?這是一個簡單的選擇鏈接,每個循環,加入查詢字符串 – epascarello

+0

你有什麼嘗試嗎?像'每個'。 – Tushar

+0

你從哪裏得到這些值? –

回答

0

使用jQuery,你可以循環頁面中的每個a標籤,然後刪除最後一個字符的href,最後追加?city=miami&tel=7777777777

$(function() { 
    $("a").each(function(i, v) { 
     var href = $(v).attr("href"); 
     href = href.substr(0, href.length - 1) + "?city=miami&tel=7777777777"; 
     $(v).attr("href", href); 
    }); 
}) 

UPDATE

既然你更新你的HTML代碼,添加id屬性您a標籤的父母,你可以應用上面的規則他們每個人,通過簡單地改變只有jQuery選擇:通過他們

$(function() { 
    $("#menu-item-3064 a, #menu-item-3042 a, #menu-item-3043 a").each(function(i, v) { 
     var href = $(v).attr("href"); 
     href = href.substr(0, href.length - 1) + "?city=miami&tel=7777777777"; 
     $(v).attr("href", href); 
    }); 
}) 
+0

非常感謝你,我很樂意使用這個,但我不想改變頁面上的所有鏈接,我只想要一個特定的3 –

+0

所以使用一個選擇器,只給你那3個。'$( '.menu a')' –

0

迭代與for。做同樣的檢查。下面這樣做沒有jQuery的要求。

// This is your array of links to find and change 
 
var linksToChange = ["http://asdf.com/home/", "http://asdf.com/busi/", "http://asdf.com/test/"] 
 

 
// some external vars 
 
var city = "miami", 
 
    tel = "7777777"; 
 

 
// Generate your string to add 
 
var stringToAdd = "?city=" + city + "&tel=" + tel; 
 

 
// Get all "a" tags 
 
var links = document.getElementsByTagName("a"); 
 

 
for(var i = 0; i<links.length; i++) { 
 
    for (var j = 0; j < linksToChange.length; j++) { 
 
    if (linksToChange[j] == links[i].href) { 
 
     // Modify link here 
 
     links[i].href = links[i].href.slice(0,-1) + stringToAdd; 
 
    } 
 
    } 
 
}
<a href="http://asdf.com/home/">1</a> 
 
<a href="http://asdf.com/busi/">2</a> 
 
<a href="http://asdf.com/other/">3</a> 
 
<a href="http://asdf.com/more/">4</a> 
 
<a href="http://asdf.com/test/">5</a>

0

您可以使用attr(fn)遍歷它們,並使用$('#menu a')作爲選擇

$('#menu a').attr('href', function(index, oldHref){ 
    return oldHref +'?city=miami&tel=7777777777' 
}); 

另外一個<a>標籤具有search財產

$('#menu a').each(function(){ 
    this.search = 'city=miami&tel=7777777777'; 
}); 

創建查詢字符串的一種簡單方法是使用$.param()

var data ={ 
    city:'miami', 
    tel : '555-555-555' 
} 

var str = $.param(data); // "city=miami&tel=555-555-555"