2012-08-15 67 views
-1

可能重複:
How to change the href for a hyperlink using jQuery覆蓋錨的href

我有一個錨標記,其中的href = 「」 像這樣 <a class="SwitchAccountLink" href=""></a>。我想用jQuery覆蓋這個,並提供一個不同的href。是否有可能重寫錨標籤的內聯href?

在此先感謝。

+0

http://api.jquery.com/attr/ – undefined 2012-08-15 16:50:38

+1

@undefined - 忘了再次初始化您的用戶名? ;) – Lix 2012-08-15 16:52:20

回答

2

您可以使用jQuery's .attr()函數來修改元素的特定屬性。

$("a.SwitchAccountLink").attr('href',NEW_URL); 

此代碼將取代SwitchAccountLink類的所有錨標籤與NEW_URL

如果您只想更改特定鏈接,則可能必須提供更獨特的id屬性或組合所有鏈接的其他類。

1

你可以改變它做$(".SwitchAccountLink").attr('href','myNewHref');

2

您可以使用jQuery attr()來獲取和設置元素的屬性值,詳細瞭解attr()作爲您的錨標記具有我們將用它來上課唯一標識所有類別的錨點SwitchAccountLink

$('.SwitchAccountLink').attr('href', 'Yourhref'); 
1

當然,請嘗試:

$('.SwitchAccountLink').attr('href','someurl');