Home  >  Article  >  php教程  >  php实现jQuery扩展函数

php实现jQuery扩展函数

WBOY
WBOYOriginal
2016-06-13 12:21:07944browse

就是contains这个函数,书上介绍说这个函数是按照元素的内容来来筛选选择的元素集,当我运行代码的时候老是报错,后来发现是函数库里没有这个函数,于是自己写了这个函数。
代码如下:

复制代码 代码如下:


function yhCheckIsIncludingValue(element , pattern)
{
var bool = false;
var childrenNodes = element.childNodes;
if (childrenNodes.length == 0)
{
if (element.nodeValue != null)
{
if (pattern.exec(element.nodeValue) != null)
{
return true;
}
}
}
if (childrenNodes.length != 0)
{
for (var i = 0 ; i {
if (bool = yhCheckIsIncludingValue(childrenNodes , pattern)) break;
}
}
return bool;
}
//在函数链应用这个函数
$.fn.contains = function(text)
{
var text = $.trim(text);
if (text == 'undefined') return this;
var pattern = new RegExp(text , 'i');
return this.filter(function(){
return yhCheckIsIncludingValue(this , pattern);
});
}


在IE浏览器上运行正常,不知道其他的浏览器会出现什么情况?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn