2014-02-28 37 views
0

我想要改變點擊時未激活的標記,但它不起作用。當我改變它.hover它仍然工作,但不是我想要的解決方案。在點擊時更改活動標記位置

我怎麼才能讓它成爲可能.click?

問候和thx求助!

// DOM Ready 
$(function() { 

/* 
    EXAMPLE ONE 
*/ 

/* Add Magic Line markup via JavaScript, because it ain't gonna work without */ 
$("#example-one").append("<li id='magic-line'></li>"); 

/* Cache it */ 
var $magicLine = $("#magic-line"); 

$magicLine 
    .width($(".selected").width()) 
    .css("left", $(".selected a").position().left) 
    .data("origLeft", $magicLine.position().left) 
    .data("origWidth", $magicLine.width()); 

$("#example-one li").find("a").click(function() { 
    $el = $(this); 
    leftPos = $el.position().left; 
    newWidth = $el.parent().width(); 

    $magicLine.stop().animate({ 
     left: leftPos, 
     width: newWidth 
    }); 
}, function() { 
    $magicLine.stop().animate({ 

    });  
}); 

}); 
+0

發佈HTML過,如果可能創造的jsfiddle演示 – iJade

+0

有可能在點擊事件但一個場景可以運行。所以可以使用mousedown和mouseup事件 –

回答

0

您的代碼片段不會更改當前單擊的元素上的類。 你應該嘗試這樣的事:

CSS

#example-one li a:hover { 
/* reset default style properties */ 
} 
#example-one li a.hover { color: red } 

的Javascript:

$("#example-one li").find("a").click(function(e) { 
    e.preventDefault(); // stop default action on this element when someone perform a click 
    var element = $(this); // assign the current clicked jquery object to element 

    if (element.hasClass('hover')) { 
     element.removeClass('hover'); 
    } else { 
     element.addClass('hover'); 
    } 
}); 
相關問題