2011-10-04 40 views
1

因此,我一直在工作,搜索和整天在桌面上敲着我的頭。我對所有這些都有所不同,所以如果我的編碼非常低級,我很抱歉。我想要做的是創建一個機制,記住當前頁面的錨點網址,但當用戶去改變國家改變國家頁面中的鏈接,使用戶回到他們所在的位置,但在新的國家目錄。JQuery循環拉動元素的特定屬性並將其添加到href

到目前爲止,我已經得到了選擇器拉動從前一頁的信息,並將其保存爲cooky。我甚至已經按照我的意願改變了網址。問題是,我給每個標籤的國家代碼的名稱。當代碼執行時,我無法讓它拉取每個名字,並將其應用到定製的href中,因爲它在頁面中移動。我只能得到第一個選擇或最後一個適用於它們的最後一個。任何有關我做錯了什麼的洞察力都會很棒。

$('#setNation').click(function(event) { 
    var fromPage = window.location.pathname 
    $.cookie("pageFrom", fromPage); 
}); 

$('.nationSelect').each(function() { 
    $('.nationSelect').attr("herf", "http://domain.com/"+$(this).attr("name")+$.cookie("pageFrom")); 
}); 

和HTML看起來這

<div id="nationList"> 
    <a name="de" class="nationSelect"> 
    <div id="nationListIcon"> 
     <div id="nationListImg" style="background-position: -229px -76px;"></div> 
     <p lang="de" xml:lang="de">Deutschland</p> 
    </div> 
    </a> 
    <a name="es" class="nationSelect"> 
    <div id="nationListIcon"> 
     <div id="nationListImg" style="background-position: -305px -76px;"></div> 
     <p lang="es" xml:lang="es">España</p> 
    </div> 
    </a> 
    <a name="fr" class="nationSelect"> 
    <div id="nationListIcon"> 
     <div id="nationListImg" style="background-position: -380px -76px;"></div> 
     <p lang="fr" xml:lang="fr">France</p> 
    </div> 
    </a> 
</div> 

感謝您的幫助

回答

1

你有一個錯字。它的href,而不是herf。 herf聽起來像是你從一個骯髒的女孩身上拾起的東西。

//Your current code changes the href for all links of class nationSelect. This means whatever the last link changed is will be applied to all links on the page 
$('.nationSelect').attr("herf", 

//What you want is for only the current one to get the new value. So use $(this) 
$('.nationSelect').each(function() { 
    $(this).attr('href', 'http://domain.com/' + $(this).attr('name') + '/morestuff'); 
}); 
相關問題