如何更改活动链接颜色而不刷新页面?

时间:2014-02-20 18:38:24

标签: javascript jquery html

我正在努力开发一个网站。我的网站有两个导航链接。当用户单击导航链接时,将加载页面而不刷新。我做了这个部分。但问题是,活动链接的颜色没有变化。这是我的网站https://dl.dropboxusercontent.com/u/168659703/project3/index.html,我使用以下代码进行页面加载

 $(function() {

var newHash      = "",
    $mainContent = $("#main-content"),
    $pageWrap    = $("#page-wra"),
    baseHeight   = 0,
    $el;

$pageWrap.height($pageWrap.height());
baseHeight = $pageWrap.height() - $mainContent.height();

$(".nav").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    return false;
});

$(window).bind('hashchange', function(){

    newHash = window.location.hash.substring(1);


    if (newHash) {
        $mainContent
            .find("#guts")
            .fadeOut(200, function() {
                $mainContent.hide().load(newHash + " #guts", function() {
                    $mainContent.fadeIn(200, function() {
                        $pageWrap.animate({
                            height: baseHeight + $mainContent.height() + "px"
                        });
                    });

                    $(".nav a").removeClass("active");
                    $(".nav a[href="+newHash+"]").addClass("active");
                });
            });
    };

});

$(window).trigger('hashchange');

});

请告诉我如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

如果我读了你正确你需要重置锚:

$("#anchor1").hide();
$("#anchor1").attr('href', some_val);
$("#anchor1").show();

或者

var href = $('#anchor1').attr("href")
$('#anchor1').removeAttr('href').prop('href', href);

等等。

强制浏览器重绘元素。

Read this.

答案 1 :(得分:0)

直接改变颜色

$(".nav").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    $(this).css('color', '#000'); // black for example
    return false;
});

或添加一个类

$(".nav").delegate("a", "click", function() {
    window.location.hash = $(this).attr("href");
    $(this).addClass('visited');
    return false;
});

CSS

.visited {
    color: #000;
}