我需要使用jquery更新頁面的href值。 請將href =「http://www.google.com?gsec=account」更改爲href =「http://account.google.com?gsec=account」我如何完成此操作。我需要使用jquery更新href值
回答
這將做替換整個,我認爲你正在尋找的頁面。
// 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"]')
。
這不會修改沒有「gsec = account」參數的google鏈接嗎? – jmar777 2010-08-11 14:44:12
@ jmar777 - 是的。我不確定該特定參數是否是要求。將選擇器更改爲'$('a [href * =「gsec = account」]')'只會關注這些元素。 – user113716 2010-08-11 14:47:07
雅...這可能是98%的安全,但當然失敗的yahoo.com?gsec =賬戶,或gsec = accountfoo等不知道如何_exact_他需要這件事。問題有些模糊。 – jmar777 2010-08-11 14:52:02
可以說你有這樣的:
<a href="http://www.ibm.com" id="myLink">
你應該使用這樣的:
var newHref = "http://google.com";
$("#myLink").attr('href', newHref);
我覺得你在你的問題遺漏了某些東西。儘管如此:
var link = $("a"); // Or some other selector to access the link
link.attr("href", "http://account.google.com?gsec=account");
使用jQuery的.attr()
方法。使用一個參數,它會返回屬性值,並使用兩個參數來設置它。
$("a#whatever").attr("href", "http://account.google.com?gsec=account");
這是一個重複:
它沒有提到How to change the href for a hyperlink using jQuery
一種情況是,如果你設置需要進行更改,然後在你的錨一個id,你可以使用id作爲jQuery中的選擇器。
$("#LinkId").attr('href', "http://account.google.com?gsec=account")
如果要更改所有的鏈接,那麼:
$(document).ready(function() {
$("a").attr("href", "http://account.google.com?gsec=account");
});
這樣應該可以提供一個非常完整的解決方案,您的問題(如盡我所能解釋它,反正):
$(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等查詢字符串變量。
- 1. 使用jQuery更新href中的querystring值
- 2. 需要更新插件使用jQuery 1.4.3
- 3. 我需要使用jQuery驗證與更新面板
- 4. jquery - 基於選擇值更新href?
- 5. 我需要使用jQuery
- 6. 我需要使用jQuery
- 7. 使用jquery更新頁面中選定的href值
- 8. 需要用新值
- 9. 使用jQuery .eq更新值
- 10. 使用jquery更新值
- 11. 使用jQuery我需要用連字符
- 12. jquery href - 需要在mac和pc上使用
- 13. 需要更換URL href和標籤
- 14. pfbc需要更改使用jquery
- 15. 用jquery更改href
- 16. 需要使用jQuery
- 17. 需要使用jQuery
- 18. 我需要使用用戶輸入動態更新iframe
- 19. 我需要使用Struts2的,jQuery插件
- 20. 如何,我需要使用jQuery
- 21. 我如何需要使用Jquery UI Block
- 22. 我需要用$(本)jQuery中
- 23. 需要更新SQLite中列的值
- 24. NSData需要更新
- 25. 我需要確保使用jquery get或post的最新響應
- 26. 我需要使用jquery在子元素中設置寬度值?
- 27. 我需要更新一個Bookmarklet的jQuery版本
- 28. 我需要在Linux上更新R版
- 29. 我們是否需要更新Orbeon?
- 30. 更改href jquery
我假設你需要一個動態的解決方案來搜索頁面,並進行替換,而不是將一個預定的'href'分配給一個預定的''''元素。如果是這樣,我在下面添加了一個答案,完成了這一點。 – user113716 2010-08-11 14:22:21
嗯......我發佈了一個假設「帳戶」是一個動態值,並且URL可能存在其他變體(例如http/https和其他查詢字符串變量)。看到你的迴應後,我可能會過度沮喪。 – jmar777 2010-08-11 14:27:43
@jmar - 是的,我的答案確實有一些關於什麼被替換的要求。在選擇器和'replace()'中使用正則表達式進行一些調整會使它更加開放。 – user113716 2010-08-11 14:35:17