jq之類名相關操作(addclass、removeClass、hasClass)及深意

addclass

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>溫故而知"心"</title>
  <link rel="stylesheet" href="./index.css">
</head>

<body>
  <div class="demo"></div>
  <div class="demo"></div>
  <div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  //給第一個類名爲demo的div添加一個active類名
  $('.demo').eq(0).addClass('active');

  //給第二個類名爲demo的div添加多個類名
  $('.demo').eq(1).addClass('active xyz');

  //給全部的類名爲demo的div添加一個state類名
  $('.demo').addClass('state');

  /*
  對於jq,addClass()方法,我們還可以當成addClass(function(){})
  */
  $('.demo').addClass(function(index, ele){
    //我們現在給爲偶數的類名爲demo的div添加一個demo2的類名
    if( (index+1) % 2 == 0){
      return 'demo2'
    }
  })
</script>

</html>

removeClass

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>溫故而知"心"</title>
  <link rel="stylesheet" href="./index.css">
</head>

<body>
  <div class="demo demo1 active"></div>
  <div class="demo demo1 active"></div>
  <div class="demo demo1"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
  //給第一個類名爲demo的div刪除一個active類名
  $('.demo').eq(0).removeClass('active');

  //給第一個類名爲demo的div刪除demo和active類名,注意順序無所謂
  $('.demo').eq(0).removeClass('active demo');

  //如果第二個類名demo使用removeClass方法,裏面什麼都不填,那就是把class裏面的類名全部都刪除掉
  $('div').eq(1).removeClass();

  //刪除body裏面的div爲偶數的(順序)
  $('div').removeClass(function (index, ele) {
    if ((index + 1) % 2 == 0) {
      return 'demo1'
    }
  })

  //刪除body裏面的div所有的類名
  $('div').removeClass()


</script>

</html>

hasClass

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>溫故而知“心”</title>
    <style>
        .demo {
            width: 100px;
            height: 100px;
            background-color: #0000ff;
            margin-bottom: 10px;
        }

        .demo.active {
            background-color: #ff0000;
        }
    </style>
</head>

<body>
    <div class="demo active"></div>
    <div class="demo"></div>
    <div class="demo"></div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    //通過jq判斷類名爲demo的div裏面有沒有active類名
    console.log($('.demo').hasClass('active'));

    /*
    點擊div並使背景顏色變成紅色,其餘的div變成藍色;
    */
    $('.demo').click(function () {
        $('.demo').removeClass('active');
        $(this).addClass('active')
    })
</script>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>溫故而知“心”</title>
    <style>
        .wrapper .style1 {
            background-color: #ff8c00;
        }

        .wrapper .style1 li {
            color: #ff0000;
        }

        .wrapper .style2 {
            background-color: #0000ff;
        }

        .wrapper .style2 li {
            color: #006400;
        }

        .wrapper.active .style1 {
            background-color: #ff0000;
        }

        .wrapper.active .style1 li {
            color: #ff8c00;
        }

        .wrapper.active .style2 {
            background-color: #006400;
        }

        .wrapper.active .style2 li {
            color: #0000ff;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <ul class="style1">
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
        <ul class="style2">
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
    </div>
</body>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script>
    $('.wrapper').click(function (ele, index) {
        if ($(this).hasClass('active')) {
            $(this).removeClass('active')
        } else {
            $(this).addClass('active')
        }
    })
</script>

</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章