添加后,JQuery立即删除了类

时间:2015-05-10 20:53:29

标签: jquery html class

我使用JQuery函数通过使用firebug检查在HTML链接元素中添加删除类JQuery实际上添加了类,然后立即删除。 有谁知道为什么? here is fiddle

cell.contentView

watch this on youtube

1 个答案:

答案 0 :(得分:0)

看起来您的链接正在重新加载页面。如果您使用的是jQuery,请将href更改为"#"阻止它重新加载index.html。如果您必须链接到另一个PHP页面,那么您应该检查PHP中的哪个页面并添加“活动”页面。在那里,在页面加载。通过PHP ...

<?php
$uri = $_SERVER('REQUEST_URI');
$class = 'nonactive';
if ($uri=='/index.php') $class = 'active';
echo '<a href="..." class="'.$class.'">...</a>'
$class = 'nonactive';
if ($uri=='/nexUri.php') $class = 'active';
echo '<a href="..." class="'.$class.'">...</a>'
$class = 'nonactive';
?>

...通过jQuery ...

$( document ).ready( function() {
    if ($(location).attr('href')=='/index.php')
        $('#indexLinkId').addClass('active');
    else if ($(location).attr('href')=='/nextUri.php')
        $('#nextUriLinkId').addClass('active');
});

http://jsfiddle.net/7yydg1nt/3/