复制代码 代码如下:
function MyClass () {}
MyClass.hasClassName = function(element, className)
{
if (!element) return;
var elementClassName = element.className;
if (elementClassName.length == 0) return false;
//用正则表达式判断多个class之间是否存在真正的class(前后空格的处理)
if (elementClassName == className || elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
return true;
return false;
};
MyClass.addClassName = function(element, className)
{
if (!element) return;
var elementClassName = element.className;
if (elementClassName.length == 0)
{
element.className = elementClassName;
return;
}
if (elementClassName == className || elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
return;
element.className = elementClassName + " " + className;
};
MyClass.removeClassName = function(element, className)
{
if (!element) return;
var elementClassName = element.className;
if (elementClassName.length == 0) return;
if(elementClassName == className)
{
element.className = "";
return;
}
if (elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
element.className = elementClassName.replace((new RegExp("(^|\\s)" + className + "(\\s|$)"))," ");
};
function MyClass () {}
MyClass.hasClassName = function(element, className)
{
if (!element) return;
var elementClassName = element.className;
if (elementClassName.length == 0) return false;
//用正则表达式判断多个class之间是否存在真正的class(前后空格的处理)
if (elementClassName == className || elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
return true;
return false;
};
MyClass.addClassName = function(element, className)
{
if (!element) return;
var elementClassName = element.className;
if (elementClassName.length == 0)
{
element.className = elementClassName;
return;
}
if (elementClassName == className || elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
return;
element.className = elementClassName + " " + className;
};
MyClass.removeClassName = function(element, className)
{
if (!element) return;
var elementClassName = element.className;
if (elementClassName.length == 0) return;
if(elementClassName == className)
{
element.className = "";
return;
}
if (elementClassName.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))
element.className = elementClassName.replace((new RegExp("(^|\\s)" + className + "(\\s|$)"))," ");
};
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“js判断样式className同时增加class或删除class”评论...