2

单击链接时如何使用jquery突出显示链接?

例如,当我点击链接 class1_1 时,我想让这个链接变成红色(或其他颜色)。

这里的javascript代码:

<script type="text/javascript">
 $(function(){
  $("#menu li").each(function(){
     $(this).click(function(event){
       var ul=$(this).children("ul")
       var span = $(this).children("span")
       if(ul.html()!=null)
       {
          if(ul.css("display")=="none")
          {
            ul.css("display","block");
            span.addClass("up")
          }else
          {
            ul.css("display","none")
            span.removeClass("up")
          }
           event.stopPropagation();
       }else
       {
         event.stopPropagation();
       }
     });
  });
  return false;
 });
</script>

这里的html代码:

<ul id="menu">

<li class="title"><span>class1 </span>
<ul>
  <li><a href="">class1_1</a></li>
   <li><a href="">class1_2</a></li>
 </ul>
 </li>
<li class="title"><span>class2</span>
   <ul>
  <li><span>class2_1</span>
   <ul>
    <li><a href="">class2_1_1</a></li>
    <li><a href="">class2_1_1</a></li>
  </ul>
  </li>
 </ul>
</li>
</ul>

也许我无法清楚地解释我的问题,我的意思是最后一个点击链接是否成功

颜色为红色,另一个链接设置为默认颜色

4

7 回答 7

8

可以使用 CSS,不需要 jQuery:

强调:

a:active {
    background-color: #FF0000;
}

更改链接颜色:

a:active {
    color: #FF0000;
}

编辑:回复您的评论...如果您的链接没有将浏览器定向到另一个页面,您可以使用Mike Robinson 的答案来完成相同的效果,而无需离开页面,也无需将颜色更改回默认的 onblur。

于 2009-05-01T20:28:24.917 回答
5

认为应该这样做,尽管我现在手头没有 jquery。假设 'up' 是一个让你的链接变红的类:

$("ul#menu a").click(function(){
 $(this).addClass('up');
});
于 2009-05-01T20:17:50.717 回答
4

这应该有效:

Javascript:

$(function(){
    $('.class1').click(function() {
        $(this).toggleClass('active1');
    });
});

CSS:

a.class1 { color: red; }
a.active1 { color: blue; }

HTML:

<a href="#" class="class1">some text</a>

最好使用 toggleClass(2 合 1)而不是 addClass/removeClass。

于 2009-05-01T20:50:38.610 回答
1

我会推荐http://plugins.jquery.com/project/color jquery.color 插件。它将允许您为各种 html 元素设置颜色动画。

于 2009-05-01T20:19:32.473 回答
1
<script type = "text/javascript" >
$(function() {
    $("#menu li").each(function() {
        $(this).click(function(event) {

            $("#menu li").removeClass("high");
            $(this).addClass("high");

            var ul = $(this).children("ul")
            var span = $(this).children("span")
            if (ul.html() != null) {
                if (ul.css("display") == "none") {
                    ul.css("display", "block");
                    span.addClass("up")
                } else {
                    ul.css("display", "none") span.removeClass("up")
                }
                event.stopPropagation();
            } else {
                event.stopPropagation();
            }
        });
    });
    return false;
});
</script>


<style>
.high{color:red}
</style> 
于 2009-05-09T10:52:54.103 回答
1

Javascript:

$('.link').click(function() {
    if (!$(this).hasClass('hi')) {
        // If this link is not already highlighted, highlight it and make
        // sure other links of class .link are not highlighted.
        $('.hi').removeClass('hi');
        $(this).addClass('hi');
    }
});

CSS:

a.hi { color: red; }

html:

<a href="#" class="link">my text</a>
<a href="#" class="link">that text</a>
<a href="#" class="link">this text</a>
于 2012-06-30T16:04:25.637 回答
0

您可以使用 CSS 伪类active来实现。它为激活的元素添加了特殊样式。

例如,您可以这样做:

a: active { color: red; }

注意,a:active 必须在 CSS 定义中的 a:hover 之后才能生效!!

于 2009-05-01T20:33:18.667 回答