简体   繁体   English

使用 jquery 添加和删除 CSS 类

[英]Adding and removing CSS classes with jquery

I am trying to resize my footer with jquery.我正在尝试使用 jquery 调整页脚的大小。 So far when I resize the window it doesn't add the class.到目前为止,当我调整窗口大小时,它没有添加类。 Am I implementing it right?我执行得对吗?

 /* My jQuery: */ $(document).ready(function() { $(window).on('resize', function(){ var win = $(this); if (win.width() > 600) { $("#anc").addClass('social-lg'); $("#ico").addClass("icon-lg"); } else { $("#anc").addClass('social-sm'); $("#ico").addClass("icon-sm"); } }); });
 /* My CSS: */ .social-lg div.col-md-12 > ul > li > a { border: 2px solid #616161; border-radius: 50%; display: inline-block; letter-spacing: normal; text-align: center; height: 4.25rem; width: 4.25rem; } .icon-lg div.col-md-12 > ul > li > a > i { padding-top: .5rem; font-size: 2em; } .social-sm div.col-md-12 > ul > li > a { border: 2px solid #616161; border-radius: 50%; display: inline-block; letter-spacing: normal; text-align: center; height: 3.25rem; width: 3.25rem; } .icon-sm div.col-md-12 > ul > li > a > i { padding-top: .5rem; font-size: 1.5em; }
 <!-- My HTML: --> <div class="row" id="footer"> <div class="col-md-12"> <ul> <li><a id="anc" class="nostyle" href="https://www.linkedin.com/in/"><i id="ico" class="fa fa-linkedin fa-2x" aria-hidden="true"></i></a></li> <li><a id="anc" class="nostyle" href="https://github.com/"><i id="ico" class="fa fa-github fa-2x" aria-hidden="true"></i></a></li> <li><a id="anc" class="nostyle" href="https://www.instagram.com/_/"><i id="ico" class="fa fa-instagram fa-2x" aria-hidden="true"></i></a></li> <li><a id="anc" class="nostyle" href="https://twitter.com/"><i id="ico" class="fa fa-twitter fa-2x" aria-hidden="true"></i></a></li> <p>Lorem Ipsum</p> </ul> </div> </div>

Edit: embedded code inside the question instead of providing a link编辑:问题内嵌入代码而不是提供链接

You have a number of same id parameters for li and i tags which prevents jquery to select all the elements of the same id, so make them classes like following 对于li和i标记,你有许多相同的id参数,这会阻止jquery选择相同id的所有元素,所以让它们像下面这样的类

<div class="row" id="footer">
            <div class="col-md-12">
                <ul>
                    <li><a class="anc nostyle" href="https://www.linkedin.com/in/"><i class="ico fa fa-linkedin fa-2x" aria-hidden="true"></i></a></li>
                    <li><a class="anc nostyle" href="https://github.com/"><i class="ico fa fa-github fa-2x" aria-hidden="true"></i></a></li>
                    <li><a class="anc nostyle" href="https://www.instagram.com/_/"><i class="ico fa fa-instagram fa-2x" aria-hidden="true"></i></a></li>
                    <li><a class="anc nostyle" href="https://twitter.com/"><i class="ico fa fa-twitter fa-2x" aria-hidden="true"></i></a></li>
                    <p>Lorem Ipsum</p>
                </ul>
            </div>
        </div>

Then use the modified javascript code 然后使用修改后的javascript代码

$(document).ready(function() {
  $(window).on('resize', function() {
    var win = $(this);
    if (win.width() > 600) {
      $(".anc").addClass('social-lg').removeClass('social-sm');
      $(".ico").addClass("icon-lg").removeClass("icon-sm");
    } else {
      $(".anc").addClass('social-sm').removeClass('social-lg');
      $(".ico").addClass("icon-sm").removeClass("icon-lg");
    }
  }).trigger("resize"); //this to force first event on load
});

You didn't include a case when there's a need to delete a one class to make another one work. 当需要删除一个类以使另一个类工作时,您没有包含一个案例。 Toggle class should fix it. 切换类应修复它。

Edit: toggle won't work in this case. 编辑:切换在这种情况下不起作用。 You have to use another solution: 你必须使用另一种解决方案:

 $(document).ready(function() { $(window).on('resize', function() { var win = $(this); if (win.width() > 600) { $("#anc").addClass('social-lg'); $("#ico").addClass("icon-lg"); $("#anc").removeClass('social-sm'); $("#ico").removeClass("icon-sm"); } else { $("#anc").addClass('social-sm'); $("#ico").addClass("icon-sm"); $("#anc").removeClass('social-lg'); $("#ico").removeClass("icon-lg"); } }); }); 

jQuery addClass() Method jQuery addClass() 方法

The jQuery addClass() method adds one or more classes to the selected elements. jQuery addClass() 方法向所选元素添加一个或多个类。

$("h1").addClass("page-header");//add your class name here

jQuery removeClass() Method jQuery removeClass() 方法

Similarly, you can remove the classes from the elements using the jQuery removeClass() method.同样,您可以使用 jQuery removeClass() 方法从元素中删除类。 The removeClass() method can remove a single class, multiple classes, or all classes at once from the selected elements. removeClass() 方法可以从所选元素中一次性删除单个类、多个类或所有类。

$("h1").removeClass("page-header");

More Informations: https://slaford.com/css/jquery-add-and-remove-css-classes/更多信息: https : //slaford.com/css/jquery-add-and-remove-css-classes/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM