點擊添加class:toggleClass()

原生JavaScript寫法

功能

點一下添加class,再點一下移除class

.box
.box
 width: 500px
 height: 300px
 border: 1px solid #000
 background-image: url(https://goo.gl/yI3IYK)
 background-size: cover
 cursor: pointer

.img
 background-image: url(https://goo.gl/MvqA8n)
 background-size: cover
$(".box").click(
 function(){
  //toggleClass裡面的class不用加.
  $(this).toggleClass("img");
 }
);

Display

Leave a Reply

Your email address will not be published. Required fields are marked *