jquery toggleclass示例

时间:2020-02-23 14:46:15  来源:igfitidea点击:

使用jQuery toggleclass方法,我们可以根据其在线添加或者删除类。
如果存在类,则会删除它,如果类不存在,则会应用。

jQuery.toggleClass( classname [, function(index,currentclass) ] [, state ] )

ClassName:必填。
它是要为其切换类函数的类名(索引,当前文):可选。
它是将使用要删除或者添加一个或者多个类名返回的函数。

索引:它将在集合中提供元素索引。

CurrentClass:所选元素的当前类。

状态:可选的.fte状态为true,仅添加类名,如果是false,则只会删除类名。

jquery toggleclass示例:

允许创建名为jquerytoggleclass.htmlHTML文件如下所示

<!doctype>
<html>
<head>
  <title>JQuery Toggle to Show/Hide Content</title>
  <script src="http://code.jquery.com/jquery-2.2.3.min.js"></script>
</head>
<h2>Jquery toggleClass example</h2>
<style>
   .divClass{
        border:3px dotted red;
        color:red;
        font-family: arial narrow;
   }
</style>
<script type="text/javascript">
   $(document).ready(function(){
      $("#mybutton").click(function(){
         $("div").toggleClass("divClass");
      });
   });
</script>
<body>
  <button id="mybutton">Click to toggle class</button>
  <div>
 Hello world from theitroad!!!!
    <br
 Welcome to JQuery.
  </div>
</body>
</html>

toggleclass示例说明:

  • $(document).ready(function():加载完全DOM时,将在此函数调用。
  • $("button").click(function(event):单击"单击"单击"按钮时会调用此函数。
  • $("div").toggleclass("divclass"):其中我们正在调用div元素的toggleclass。