2012-08-06 68 views
0

每次單擊某個元素時,是否有任何方法可以在主體類上獲取項目列表? 林到現在這一權利:單擊元素時更改主體類

$("#step a").click(function (i) { 
    i = i+1; 
    $("body").addClass("item" i); 
}); 
+2

什麼**的身體類獲得一個項目列表**手段? – Shyju 2012-08-06 12:52:59

+0

你錯過了一個串接''item「+ i' – 2012-08-06 18:10:06

回答

0
var i = 0; 
$("#step a").click(function() { 
    i = i+1; 
    $('body').removeClass().addClass('item' + i); 
}); 
0

你點擊功能將通過您的點擊功能來傳遞事件。你不能加1。

你應該在你的html中使用類似的東西。數據屬性將步數存儲在錨爲您提供:

<div id="step"> 
    <a data-step="1">Click me</a> 
</div> 

而且一些JavaScript來控制它:

$("#step a").click(function(e) { 
    e.preventDefault(); //Stop browser jumping to top of the page 

    var step = $(this).attr("data-step"); //Get the current step from attribute 
    $("body").addClass("item-" + step); //Add this class to the body 

});