【问题标题】:How do I modify this jQuery to add an active class to the selected link? [duplicate]如何修改此 jQuery 以将活动类添加到所选链接? [复制]
【发布时间】:2016-07-21 14:10:18
【问题描述】:
jQuery(document).ready(function() {
    jQuery(".page-contents div").hide();
          // Show chosen div, and hide all others
        jQuery("a").click(function (e) {
            //e.preventDefault();
            jQuery("#" + jQuery(this).attr("class")).show().siblings('div').hide();
        });
   });

http://jsfiddle.net/fXE9p/

有人能解释一下我是如何修改这段代码的,以便将“活动”类添加到所选链接,以及它是如何工作的吗?提前谢谢你

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    以下代码将active 类添加到点击的链接:

    jQuery(document).ready(function() {
        jQuery(".page-contents div").hide();
        // Show chosen div, and hide all others
        jQuery("a").click(function (e) {
            //e.preventDefault();
            //jQuery("#" + jQuery(this).attr("class")).show().siblings('div').hide();
          jQuery(this).addClass("active");
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 2013-08-15
      • 2016-03-28
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多