Categories
JqueryUI教程

JqueryUI-切换类

JqueryUI-切换类


本章将讨论toggleClass()方法,这是一个有用的新类。 toggleClass()方法从匹配的元素集中的每个元素添加或删除一个或多个类。

句法

在jQueryUI的1.0版中添加

toggleClass()方法的基本语法如下:

.toggleClass( className [, switch ] [, duration ] [, easing ] [, complete ] )
Sr.No. Parameter & Description
1

className

This is a String and represents the CSS class name, or space-delimited list of class names, to be added, removed, or toggled.

2

switch

This is of type Boolean and if specified, forces the toggleClass() method to add the class if true, or to remove the class if false.

3

duration

This is of type Number or String and optionally provides one of slow, normal, fast, or the duration of the effect in milliseconds. If omitted, the animate() method determines the default. Its default value is 400.

4

easing

The name of the easing function to be passed to the animate() method.

5

complete

This is a callback method called for each element when the effect is complete for this element.

在jQueryUI 1.9版本中添加

在1.9版中,此方法现在支持选项,该选项还将为后代元素设置动画。

.toggleClass( className [, switch ] [, options ] )
Sr.No. Parameter & Description
1

className

This is a String and represents the CSS class name, or space-delimited list of class names, to be added, removed, or toggled.

2

switch

This is of type Boolean and if specified, forces the toggleClass() method to add the class if true, or to remove the class if false.

3

options

This represents all animation settings. All properties are optional. Possible values are −

  • duration − A string or number determining how long the animation will run.. Its default value is 400.

  • easing − A string indicating which easing function to use for the transition. Its default value is swing. Possible values are here.

  • complete − This is a callback method called for each element when the effect is complete for this element.

  • children − This is a Boolean and represents whether the animation should additionally be applied to all descendants of the matched elements.

  • queue − This is of type String/Boolean indicating whether to place the animation in the effects queue.

例子

以下示例演示了toggleClass()方法的用法。

<meta charset="utf-8">
      <title>jQuery UI Switch Class Example</title>
      <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .class1 {
            border-width : 10px;
            border-color : grey;
            background-color : #cedc98;
            color : black;
         }
      </style>
      
      <script>
         function toggle () {
            $("#para").toggleClass ("class1", 1000);
         }
      </script>
   
   
   
      <button onclick="toggle()"> Toggle </button>
      <p id="para" style="border-style:solid"> Welcome to Tutorials Point </p>
   

让我们将上面的代码保存在HTML文件toggleclassexample.htm中,并在支持javascript的标准浏览器中将其打开,您还必须看到以下输出。现在,您可以处理结果了-

单击“切换”按钮以查看如何更改文本的CSS类。

error: 内容受到保护 !!