javascript 如何判断元素是否包含一个特定的类,hasClass函数的实现
2013-12-13 07:36
866 查看
刚开始这么写的:
用\b匹配word boundary,这样可以有效处理"aaa", "aaa bbb"的匹配,但是忘记了word boundary的明确定义
Before the first character in the string, if the first character is a word character.
After the last character in the string, if the last character is a word character.
Between two characters in the string, where one is a word character and the other is not a word character.
在简单的测试中工作得很好,后来写的程序总是不能按照预期的想法工作,花了不少时间才知道原来是这里出了问题:想当然认为word boundary只包括上面的1,2和空格" ",其实
A "word character" is a character that can be used to form words. All characters that are not "word characters" are "non-word characters".
Exactly which characters are word characters depends on the regex flavor you're working with. In most flavors, characters that are matched by the short-hand character class \w are the characters that are treated as word characters by word boundaries.
当我在class为"account__item--active"的元素中查找"account__item"时函数返回true,然后程序悄悄在这里就出现了问题。这并不是我想要的。后来看了jquery1.10和2.0.3它的主要逻辑是这样实现的:
function hasClass(element, cls) { var regexp = new RegExp("\\b" + cls + "\\b"); return element.className.search(regexp) !== -1; } // end hasClass()
用\b匹配word boundary,这样可以有效处理"aaa", "aaa bbb"的匹配,但是忘记了word boundary的明确定义
Before the first character in the string, if the first character is a word character.
After the last character in the string, if the last character is a word character.
Between two characters in the string, where one is a word character and the other is not a word character.
在简单的测试中工作得很好,后来写的程序总是不能按照预期的想法工作,花了不少时间才知道原来是这里出了问题:想当然认为word boundary只包括上面的1,2和空格" ",其实
A "word character" is a character that can be used to form words. All characters that are not "word characters" are "non-word characters".
Exactly which characters are word characters depends on the regex flavor you're working with. In most flavors, characters that are matched by the short-hand character class \w are the characters that are treated as word characters by word boundaries.
当我在class为"account__item--active"的元素中查找"account__item"时函数返回true,然后程序悄悄在这里就出现了问题。这并不是我想要的。后来看了jquery1.10和2.0.3它的主要逻辑是这样实现的:
function hasClass(element, cls) { var className = " " + cls + " ", rclass = /[\t\n\r\f]/g; if (element.nodeType === 1 && (" " + element.className + " ").replace(rclass, " ").indexOf(className) >= 0) { return true; } // end if return false; } // end hasClass()
相关文章推荐
- javascript 判断数组是否已包含了某个元素的函数
- javascript中如何判断一个元素在页面中是否存在(转)
- javascript 判断数组是否已包含了某个元素的函数
- 怎么使用jquery判断一个元素是否含有一个指定的类(class)另外如何给元素动态添加类
- C#实现如何判断一个数组中是否有重复的元素
- 编写js扩展方法实现判断一个数组中是否包含某个元素
- 判断数组是否包含某个元素的js函数实现方法
- javascript(jquery)中如何判断一个元素在页面中是否存在
- 编写js扩展方法实现判断一个数组中是否包含某个元素
- 在JavaScript中如何判断一个字符串中是否包含某个字符串
- 判断数组是否包含某个元素的js函数实现方法
- javascript如何检查网页元素是否包含某个特定字符串
- 编写一个函数实现对单链表是否有环的判断
- html元素是否包含另外一个元素,以及classList属性
- php如何判断一个字符串是否包含另一个字符串
- JavaScript中如何检测一个变量是一个String类型?请写出函数实现
- 编写js扩展方法判断一个数组中是否包含某个元素
- Jquery判断元素下是否包含特定元素
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
- javascript中按位操作的应用,如何快速取整 判断字符串是否是包含某字符串