开发者

Dynamic underlining of active page

开发者 https://www.devze.com 2023-03-26 13:46 出处:网络
Asked a similar question a while ago and tried 开发者_Python百科to build off of my answer but am still having trouble.

Asked a similar question a while ago and tried 开发者_Python百科to build off of my answer but am still having trouble.

I've got a navigation menu that links to different places within the page. I'd like the active pane's link to be underlined. See the jsFiddle for demonstration. The return false is a necessary part of the code. I have a javascript function guiding the page to the location instead of jumping to it instantly.

Thank you!

http://jsfiddle.net/danielredwood/aBuZu/3/

HTML

<div id="nav">
    <a href="#about" id="nav_about">ABOUT</a><br />
    <a href="#pictures" id="nav_pictures">PICTURES</a><br />
    <a href="#contact" id="nav_contact">CONTACT</a>
</div>

CSS

a, a:active, a:visited {
    color:#1d1d1d;
    text-decoration:none;
}
a:hover {
    text-decoration:underline;
}

JavaScript

$('#nav a').click(function(){
    $('#nav a').css('text-decoration', 'none', function(){
        $(this).css('text-decoration', 'underline');
    });
    return false;
});


Try this http://jsfiddle.net/aBuZu/1/

 $('#nav a').click(function(){
    $('#nav a').css("textDecoration", "none"); 
    $(this).css('textDecoration', 'underline');
    return false;
 });


Easier:

$('#nav a').click(function(event) {
    event.preventDefault(); //same as return false
    $('#nav a').removeClass('active');        
    $(this).toggleClass('active');   
});

CSS:

a {
    color:#1d1d1d;
    text-decoration:none;
}
a:hover, a.active {
    text-decoration:underline;
}


$(this).css('color', 'underline');

is kinda nonsense. Perhaps color should be text-decoration: Example


$('#nav a').click(function(e){
    $('#nav a').css('text-decoration', 'none');
    $(this).css('text-decoration', 'underline');
    e.preventDefault();
});


First you need to choose framework jQuery in jsFiddle

Updated JS

$('#nav a').click(function(){
    $('#nav a').css('text-decoration', 'none');
    $(this).css('text-decoration', 'underline');
    return false;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号