2010-08-11 66 views
2

我需要使用jquery更新頁面的href值。 請將href =「http://www.google.com?gsec=account」更改爲href =「http://account.google.com?gsec=account」我如何完成此操作。我需要使用jquery更新href值

+0

我假設你需要一個動態的解決方案來搜索頁面,並進行替換,而不是將一個預定的'href'分配給一個預定的''''元素。如果是這樣,我在下面添加了一個答案,完成了這一點。 – user113716 2010-08-11 14:22:21

+0

嗯......我發佈了一個假設「帳戶」是一個動態值,並且URL可能存在其他變體(例如http/https和其他查詢字符串變量)。看到你的迴應後,我可能會過度沮喪。 – jmar777 2010-08-11 14:27:43

+0

@jmar - 是的,我的答案確實有一些關於什麼被替換的要求。在選擇器和'replace()'中使用正則表達式進行一些調整會使它更加開放。 – user113716 2010-08-11 14:35:17

回答

7

這將做替換整個,我認爲你正在尋找的頁面。

// Find `<a>` elements that contain `www.google.com` in the `href`. 
$('a[href*="www.google.com"]').attr('href', function(i,href) { 
     // return a version of the href that replaces "www." with "accounts." 
    return href.replace('www.', 'accounts.'); 
}); 

試試看:http://jsfiddle.net/dT8j6/


編輯:該版本允許https://並沒有www.鏈接。

試試看:http://jsfiddle.net/dT8j6/1/

$('a[href*="google.com"]').attr('href', function(i,href) { 
    return href.replace(/^http(s*):\/\/(www\.)*google.com/, 'http$1://accounts.google.com'); 
}); 

編輯:如果你只是想改變這種有gsec=account元素,然後選擇更改爲$('a[href*="gsec=account"]')

+0

這不會修改沒有「gsec = account」參數的google鏈接嗎? – jmar777 2010-08-11 14:44:12

+0

@ jmar777 - 是的。我不確定該特定參數是否是要求。將選擇器更改爲'$('a [href * =「gsec = account」]')'只會關注這些元素。 – user113716 2010-08-11 14:47:07

+0

雅...這可能是98%的安全,但當然失敗的yahoo.com?gsec =賬戶,或gsec = accountfoo等不知道如何_exact_他需要這件事。問題有些模糊。 – jmar777 2010-08-11 14:52:02

3

可以說你有這樣的:

<a href="http://www.ibm.com" id="myLink"> 

你應該使用這樣的:

var newHref = "http://google.com"; 

$("#myLink").attr('href', newHref); 
0

我覺得你在你的問題遺漏了某些東西。儘管如此:

var link = $("a"); // Or some other selector to access the link 
link.attr("href", "http://account.google.com?gsec=account"); 
0

使用jQuery的.attr()方法。使用一個參數,它會返回屬性值,並使用兩個參數來設置它。

$("a#whatever").attr("href", "http://account.google.com?gsec=account"); 
1

這是一個重複:

它沒有提到How to change the href for a hyperlink using jQuery

一種情況是,如果你設置需要進行更改,然後在你的錨一個id,你可以使用id作爲jQuery中的選擇器。

$("#LinkId").attr('href', "http://account.google.com?gsec=account") 
0

如果要更改所有的鏈接,那麼:

$(document).ready(function() { 
$("a").attr("href", "http://account.google.com?gsec=account"); 
}); 
1

這樣應該可以提供一個非常完整的解決方案,您的問題(如盡我所能解釋它,反正):

$(function() { 
    $('a').each(function() { 
    var $this = $(this), 
     href = $this.attr('href'); 
    var res = href.match(/(.*?)(www)(\.google\.com.*?)([?&]gsec=)([^&]+)(.*)/); 
    if (null != res) { 
     // remove the "full match" entry 
     res = res.slice(1); 
     // replace www match with account match 
     res[1] = res[4]; 
     // update the href attribute 
     $this.attr('href', res.join('')) 
    } 
    }); 
}); 


編輯:如果「賬戶」是一個靜態值,那麼這個也能發揮作用:

$(function() { 
    $('a').each(function() { 
    var $this = $(this), 
     href = $this.attr('href'); 
    var res = href.match(/(.*?\/\/)(www)(\.google\.com.*?)([?&]gsec=account)(&?.*)/); 
    if (null != res) { 
     // remove the "full match" entry 
     res = res.slice(1); 
     // replace www match with account match 
     res[1] = 'account'; 
     // update the href attribute 
     $this.attr('href', res.join('')) 
    } 
    }); 
}); 

請注意,這些解決方案假定有可能在URL其他變化,如HTTP/HTTPS等查詢字符串變量。