溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

javascript中.toggleClass()怎么用

發(fā)布時間:2022-03-05 11:23:46 來源:億速云 閱讀:200 作者:小新 欄目:web開發(fā)

這篇文章主要介紹javascript中.toggleClass()怎么用,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

  如果匹配元素的父級元素有bar樣式類名,這個例子將為<div class="foo">元素切換 happy 樣式類; 否則他將切換 sad 樣式類 。

  例子:

  Example: 當(dāng)點擊段落的是有切換 'highlight' 樣式類

  <!DOCTYPE html>

  <html>

  <head>

  <style>

  p { margin: 4px; font-size:16px; font-weight:bolder;

  cursor:pointer; }

  .blue { color:blue; }

  .highlight { background:yellow; }

  </style>

  <script src="https://code.jquery.com/jquery-latest.js"></script>

  </head>

  <body>

  <p class="blue">Click to toggle</p>

  <p class="blue highlight">highlight</p>

  <p class="blue">on these</p>

  <p class="blue">paragraphs</p>

  <script>

  $("p").click(function () {

  $(this).toggleClass("highlight");

  });

  </script>

  </body>

  </html>

  Example: 每當(dāng)?shù)谌吸c擊段落的時候添加 "highlight" 樣式類, 第一次和第二次點擊的時候移除 "highlight" 樣式類

  <!DOCTYPE html>

  <html>

  <head>

  <style>

  p { margin: 4px; font-size:16px; font-weight:bolder;

  cursor:pointer; }

  .blue { color:blue; }

  .highlight { background:red; }

  </style>

  <script src="https://code.jquery.com/jquery-latest.js"></script>

  </head>

  <body>

  <p class="blue">Click to toggle (<span>clicks: 0</span>)</p>

  <p class="blue highlight">highlight (<span>clicks: 0</span>)</p>

  <p class="blue">on these (<span>clicks: 0</span>)</p>

  <p class="blue">paragraphs (<span>clicks: 0</span>)</p>

  <script>

  var count = 0;

  $("p").each(function() {

  var $thisParagraph = $(this);

  var count = 0;

  $thisParagraph.click(function() {

  count++;

  $thisParagraph.find("span").text('clicks: ' + count);

  $thisParagraph.toggleClass("highlight", count % 3 == 0);

  });

  });

  </script>

  </body>

  </html>

  Example: Toggle the class name(s) indicated on the buttons for each div.

  <!DOCTYPE html>

  <html>

  <head>

  <style>

  .wrap > div { float: left; width: 100px; margin: 1em 1em 0 0;

  padding=left: 3px; border: 1px solid #abc; }

  div.a { background-color: aqua; }

  div.b { background-color: burlywood; }

  div.c { background-color: cornsilk; }

  </style>

  <script src="https://code.jquery.com/jquery-latest.js"></script>

  </head>

  <body>

  <div class="buttons">

  <button>toggle</button>

  <button class="a">toggle a</button>

  <button class="a b">toggle a b</button>

  <button class="a b c">toggle a b c</button>

  <a href="#">reset</a>

  </div>

  <div class="wrap">

  <div></div>

  <div class="b"></div>

  <div class="a b"></div>

  <div class="a c"></div>

  </div>

  <script>

  var cls = ['', 'a', 'a b', 'a b c'];

  var divs = $('div.wrap').children();

  var appendClass = function() {

  divs.append(function() {

  return '<div>' + (this.className || 'none') + '</div>';

  });

  };

  appendClass();

  $('button').bind('click', function() {

  var tc = this.className || undefined;

  divs.toggleClass(tc);

  appendClass();

  });

  $('a').bind('click', function(event) {

  event.preventDefault();

  divs.empty().each(function(i) {

  this.className = cls[i];

  });

  appendClass();

  });

  </script>

  </body>

  </html>

以上是“javascript中.toggleClass()怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI