这篇文章主要介绍了JS怎么仿JQuery选择器功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇JS怎么仿JQuery选择器功能文章都会有所收获,下面我们一起来看看吧。

创新互联公司是一家专业提供永嘉企业网站建设,专注与成都网站建设、网站建设、HTML5、小程序制作等业务。10年已为永嘉众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
JQuery作为应用最广的JS库,其最强大的功能之一就是几乎涵盖所有方法的且代码十分简短的选择器功能,我们也可用自己的代码实现此功能,代码逻辑、使用方法与JQuery一致
function ZQuery(arg){
this.elements = []; //存东西
this.domString = ''; //保存字符串标签
if(typeof arg=='function'){
//DOMReady
DOMReady(arg);
}else if(typeof arg=='string'||arg instanceof String){
if(arg.indexOf('<')!=-1){
this.domString = arg;
}else{
//获取元素
this.elements = getEle(arg);
this.length = this.elements.length;
}
}else{
//原生对象-》ZQuery对象
this.elements.push(arg);
this.length = this.elements.length;
}
}
ZQuery.prototype.css = function(name,value){
if(arguments.length==2){
//设置一个样式
for(var i=0;i=200&&oAjax.status<300||oAjax.status==304){
json.success&&json.success(oAjax.responseText);
}else{
json.error&&json.error(oAjax.status);
}
}
};
}
function jsonp(json){
json = json||{};
if(!json.url)return;
json.timeout = json.timeout||15000;
json.cbName = json.cbName||'cb';
json.data = json.data||{};
json.data[json.cbName] = 'show'+Math.random();
json.data[json.cbName] = json.data[json.cbName].replace('.','');
json.timer = setTimeout(function(){
window[json.data[json.cbName]] = function(res){
oHead.removeChild(oS);
json.error&&json.error('网络超时!');
}
},json.timeout);
window[json.data[json.cbName]] = function(res){
clearTimeout(json.timer);
oHead.removeChild(oS);
json.success&&json.success(res);
}
var oHead = document.getElementsByTagName('head')[0];
var oS = document.createElement('script');
oS.src = json.url+'?'+json2url(json.data);
oHead.appendChild(oS);
}
function getStyle(obj,sName){
return (obj.currentStyle||getComputedStyle(obj,false))[sName];
}
function addEvent(obj,sEv,fn){
if(obj.addEventListener){
obj.addEventListener(sEv,function(ev){
var oEvent = ev||event;
if(fn.call(obj,oEvent)==false){
oEvent.cancelBubble = true;
oEvent.preventDefault&&oEvent.preventDefault();
}
},false);
}else{
obj.attachEvent('on'+sEv,function(){
var oEvent = ev||event;
if(fn.call(obj,oEvent)==false){
oEvent.cancelBubble = true;
return false;
}
});
}
}
function DOMReady(fn){
if(document.addEventListener){
addEvent(document,'DOMContentLoaded',function(){
fn&&fn();
});
}else{
addEvent(document,'onreadystatechange',function(){
if(document.readyState=='complete'){
fn&&fn();
}
});
}
}
function getByClass(oParent,sClass){
if(oParent.getElementsByClassName){
return oParent.getElementsByClassName(sClass);
}else{
var aResult = [];
var aEle = oParent.getElementsByTagName('*');
var re = new RegExp('\\b'+sClass+'\\b','g');
for(var i=0;i关于“JS怎么仿JQuery选择器功能”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“JS怎么仿JQuery选择器功能”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。
网站题目:JS怎么仿JQuery选择器功能
本文链接:http://www.scjierui.cn/article/ppcgdj.html