一、与类相关的扩展
class属性的应用极其广泛,与class的相关的操作也越来越简化,HTML5增加了 getElementsByClassName来查找元素,通过也增加了classList属性,方便我们对class进行操作,下面总结了一个ClassUtil :
1 var ClassUtil = { 2 addClass: function (ele, className) { 3 ele.classList.add(className); 4 }, 5 removeClass: function (ele, className) { 6 ele.classList.remove(className); 7 }, 8 toggleClass: function (ele, className) { 9 ele.classList.toggle(className); 10 }, 11 hasClass: function (ele, className) { 12 return ele.classList.contains(className); 13 }, 14 getClassName: function (ele) { 15 return ele.className.split(/s+/); 16 } 17 }
注意:支持classList属性的有chrome,FireFox3.6+和IE10+