zoukankan      html  css  js  c++  java
  • HTML5 classList API

    Having thrust myself into the world of JavaScript and JavaScript Libraries, I've often wondered: When are browser vendors going to see the helper methods/libraries created by the JavaScript toolkits and implement these functionalities natively within the browser? I realize that standards are important and browser vendors can't afford to half-ass these implementations but I do believe they could be...expedited.  The good news is that one of these functionalities has been add to the HTML5 API; classList.

    The classList object, added to all nodes within the DOM, provides developers methods by which to add, remove, and toggle CSS classes on a node.  classList also allows developers to check if a CSS class has been assigned to a given node.

    node.classList

    copy{
      length: {number}, /* # of class on this element */
      add: function() { [native code] },
      contains: function() { [native code] },
      item: function() { [native code] }, /* by index */
      remove: function() { [native code] },
      toggle: function() { [native code] }
    }

    node.classList, as you can see, is a small but useful collection of methods.

    Adding a CSS Class

    copymyDiv.classList.add('myCssClass');

    Removing a CSS Class

    copymyDiv.classList.remove('myCssClass');

    Toggling a CSS Class

    copymyDiv.classList.toggle('myCssClass'); //now it's added
    myDiv.classList.toggle('myCssClass'); //now it's removed

    Note: If toggle is called and the element does not have the provided CSS class, the class is added.

    Contains CSS Class Check

    copymyDiv.classList.contains('myCssClass'); //returns true or false

    Mozilla Firefox is the only browser that currently supports the classList API.  As more browsers add classList support, look for the JavaScript libraries to include classList checks instead of parsing an element's class attribute!

  • 相关阅读:
    Linux Shell for循环写法总结 皇星客栈
    关于adr指令的理解 皇星客栈
    lds linux下的通用链接脚本 皇星客栈
    2430实验点对点通信实验 皇星客栈
    #pragma vector 皇星客栈
    linux下firefox安装Adobe Flash Player插件 皇星客栈
    一个shell脚本引发的对于分号(;)和$#的使用说明(转载) 皇星客栈
    代码打开wince自带的wif配置窗口i
    C#数组的合并拆分
    Coding4Fun
  • 原文地址:https://www.cnblogs.com/canphp/p/2272376.html
Copyright © 2011-2022 走看看