相容IE6+,因IE6、IE7、IE8不支持`Array.prototype.indexOf()`和`String.prototype.trim()`,分別用`Polyfill`實現支持。 ...
相容IE6+,因IE6、IE7、IE8不支持Array.prototype.indexOf()
和String.prototype.trim()
,分別用Polyfill
實現支持。
詳細:
indexOf
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
trim
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
由於現代瀏覽器都基本支持了classList
,所以移動端可以直接使用classList
會更方便一些,如:
<div class="foo bar" id="div"></div>
var div = document.getElementById('div');
div.classList.remove("foo");
div.classList.add("anotherclass");
div.classList.toggle("visible");
div.classList.contains("foo"); //
div.classList.add("foo","bar"); //Android4.3不支持
- 應該避免直接使用多個參數,支持不全面。
SVG
和MathML
元素支持度也不全面(這個坑下麵的代碼並未填上,請熟知,有興趣的可以查下Zepto
是怎麼填坑的)。
classList
支持明細: http://caniuse.com/#search=classList
代碼
Polyfill
// 讓低版本IE支持 Array.prototype.indexOf
// Polyfill (indexOf @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
var len = o.length >>> 0;
if (len === 0) {
return -1;
}
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in o && o[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
// 讓低版本IE支持 String.prototype.trim()
// Polyfill (trim @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)
if (!String.prototype.trim) {
String.prototype.trim = function(){
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
支持 classList
實現方式
function ctrlClass(opts) {
if (!opts.ele || !opts.c) return false;
// console.log(opts.c)
var c = null;
typeof (opts.c) === 'string' ?
c = opts.c.trim().replace(/\s+/g, ' ').split(' ') :
c = opts.c; //修複不規範傳參
return opts.fun({
ele: opts.ele,
c: c
});
opts.ele = null;
}
/**
* hasClass
* @param (element, 'c1 c2 c3 c4 c5')
*/
function hasClass(ele, c) {
return ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
return opts.c.every(function(v) {
return !!opts.ele.classList.contains(v);
});
}
});
}
/**
* addClass
* @param (element, 'c1 c2 c3 c4 c5')
*/
function addClass(ele, c) {
return ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
c.forEach(function(v) {
if (!hasClass(ele, v)) {
ele.classList.add(v);
}
});
}
})
}
/**
* removeClass
* @param (element, 'c1 c2 c3')
*/
function removeClass(ele, c) {
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
c.forEach(function(v) {
// TODO 是否有必要判斷 hasClass
// if (!hasClass(ele, v)) {
ele.classList.remove(v);
// }
});
}
});
}
/**
* toggleClass
* @param (element, 'c1 c2 c3')
*/
function toggleClass(ele, c) {
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
c.forEach(function(v) {
ele.classList.toggle(v);
})
}
})
}
不支持 classList
實現方式
/**
* hasClass
* @param (element, 'c1 c2 c3 c4 c5')
*/
function hasClass(ele, c) {
return ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var cln = opts.ele.className.split(' ');
var c = opts.c;
for (var i = 0; i < c.length; i++) {
if(cln.indexOf(c[i]) !== -1){
return true;
}
}
return false;
}
});
}
/**
* addClass
* @param (element, 'c1 c2 c3')
*/
function addClass(ele, c) {
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
for (var i = 0; i < c.length; i++) {
if(!hasClass(ele, c[i])) {
ele.className = ele.className !== '' ? (ele.className + ' ' + c[i]) : c[i];
}
}
}
});
}
/**
* removeClass
* @param (element, 'c1 c2 c3')
*/
function removeClass(ele, c) {
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c,
cln = ele.className.split(' ');
for (var i = 0; i < c.length; i++) {
if (hasClass(ele, c[i])) {
cln.splice(cln.indexOf(c[i]), 1);
}
}
ele.className = cln.join(' ');
}
});
}
/**
* toggleClass
* @param (element, 'c1 c2 c3')
*/
function toggleClass(ele, c){
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
for (var i = 0; i < c.length; i++) {
!!hasClass(ele, c[i]) ? removeClass(ele, c[i]) : addClass(ele, c[i]);
}
}
});
}
完整代碼
// Polyfill (indexOf @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
var k;
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
var len = o.length >>> 0;
if (len === 0) {
return -1;
}
var n = +fromIndex || 0;
if (Math.abs(n) === Infinity) {
n = 0;
}
if (n >= len) {
return -1;
}
k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (k in o && o[k] === searchElement) {
return k;
}
k++;
}
return -1;
};
}
// Polyfill (trim @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)
if (!String.prototype.trim) {
String.prototype.trim = function(){
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
var cl = ('classList' in document.createElement('a'));
function ctrlClass(opts) {
if (!opts.ele || !opts.c) return false;
// console.log(opts.c)
var c = null;
typeof (opts.c) === 'string' ?
c = opts.c.trim().replace(/\s+/g, ' ').split(' ') :
c = opts.c; //修複不規範傳參
return opts.fun({
ele: opts.ele,
c: c
});
opts.ele = null;
}
// 支持 classList
if(cl){
/**
* hasClass
* @param (element, 'c1 c2 c3 c4 c5')
*/
function hasClass(ele, c) {
return ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
return opts.c.every(function(v) {
return !!opts.ele.classList.contains(v);
});
}
});
}
/**
* addClass
* @param (element, 'c1 c2 c3 c4 c5')
*/
function addClass(ele, c) {
return ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
c.forEach(function(v) {
if (!hasClass(ele, v)) {
ele.classList.add(v);
}
});
}
})
}
/**
* removeClass
* @param (element, 'c1 c2 c3')
*/
function removeClass(ele, c) {
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
c.forEach(function(v) {
// TODO 是否有必要判斷 hasClass
// if (!hasClass(ele, v)) {
ele.classList.remove(v);
// }
});
}
});
}
/**
* toggleClass
* @param (element, 'c1 c2 c3')
*/
function toggleClass(ele, c) {
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
c.forEach(function(v) {
ele.classList.toggle(v);
})
}
})
}
}else{
/**
* hasClass
* @param (element, 'c1 c2 c3 c4 c5')
*/
function hasClass(ele, c) {
return ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var cln = opts.ele.className.split(' ');
var c = opts.c;
for (var i = 0; i < c.length; i++) {
if(cln.indexOf(c[i]) !== -1){
return true;
}
}
return false;
}
});
}
/**
* addClass
* @param (element, 'c1 c2 c3')
*/
function addClass(ele, c) {
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
for (var i = 0; i < c.length; i++) {
if(!hasClass(ele, c[i])) {
ele.className = ele.className !== '' ? (ele.className + ' ' + c[i]) : c[i];
}
}
}
});
}
/**
* removeClass
* @param (element, 'c1 c2 c3')
*/
function removeClass(ele, c) {
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c,
cln = ele.className.split(' ');
for (var i = 0; i < c.length; i++) {
if (hasClass(ele, c[i])) {
cln.splice(cln.indexOf(c[i]), 1);
}
}
ele.className = cln.join(' ');
}
});
}
/**
* toggleClass
* @param (element, 'c1 c2 c3')
*/
function toggleClass(ele, c){
ctrlClass({
ele: ele,
c: c,
fun: function(opts) {
var ele = opts.ele,
c = opts.c;
for (var i = 0; i < c.length; i++) {
!!hasClass(ele, c[i]) ? removeClass(ele, c[i]) : addClass(ele, c[i]);
}
}
});
}
}
使用
var ele = document.getElementById('test');
hasClass(ele, 'c1');
addClass(ele, 'c1 c2 c3');
removeClass(ele, 'c1 c2 c3');
toggleClass(ele, 'c1 c2 c3');
項目
https://github.com/givebest/ctrl-cssClass