jQuery获取并设置CSS类
<!DOCTYPE html> <html> <head> <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <title>jquery访问css类</title> <style> .lay-table-cell{ width:100px; height:150px; } .box{ border:aqua; } .text{ font-weight: bold } </style> </head> <body> <div class="lay-table-cell"> <p>jquery</p> </div> <script> $(function(){ //1 获取该类某个属性 console.log($('.lay-table-cell').css('height')); //2 设置单个属性 $('.lay-table-cell').css('background', 'red'); //3 设置多个属性 $('.lay-table-cell').css({'font-size': '25px','color': '#fff'}); //4 向被选元素添加一个或多个类 $('.lay-table-cell').addClass('box'); //5 向被选元素删除一个或多个类 $('.lay-table-cell').removeClass('box'); //6 检查被选元素是否包含指定的class console.log($('.lay-table-cell').hasClass('text')); $('.lay-table-cell').click(function() { if ($(this).hasClass('text')) { $(this).removeClass('text'); } else { $(this).addClass('text'); } console.log($('.lay-table-cell').hasClass('text')); }) }) </script> </body> </html>
原文参考:https://blog.csdn.net/weixin_33751984/article/details/119401675