15--Jquery's operation on styles uses addClass() to add styles

Note: the difference between the addClass() method and the attr() method: the
former is to add a style on the basis of the original style, and the latter is to replace the original style or set a style.
If you add multiple class attribute values ​​to an element, it is equivalent to merging their styles. If different classes set the same style attributes, the latter will override the former.
Example 1:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Jquery对样式的操作</title>
    <script src="jquery-1.11.3.js"></script>

    <style>
        .myClass{
     
     
            font-weight: 900;
            color:red;
        }
        .another{
     
     
            font-style: italic;
            color: blue;
        }
    </style>
</head>
<body>
    <p title="你喜欢的运动" class="myClass">请选择你喜欢的运动?</p>
    <ul id="sport">
        <li>足球</li>
        <li>王者荣耀</li>
        <li>乒乓球</li>
        <li>排球</li>
    </ul>
     <script>
         //获取样式
         /*$(function(){
            var $_class=$("p").attr("class");
             alert($_class)
         });*/
         //设置样式: 是将原来的样式替换为新的样式
         $(function(){
     
     
             $("p").attr("class","another");
         });
     </script>
</body>
</html>

For the use of attr() method, please pay attention to https://blog.csdn.net/qwy715229258163/article/details/113876646

Guess you like

Origin blog.csdn.net/qwy715229258163/article/details/113878487