这篇文章主要介绍“JavaScript下jquery的使用方法”,在日常操作中,相信很多人在JavaScript下jquery的使用方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”JavaScript下jquery的使用方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
本文粗燥的实现jquery
的ready、each、bind、``$.fn.extend、$.extend
初始化$
(function (win) {
var _$ = function (selector, context) {
return new _$.prototype.Init(selector, context);
};
_$.prototype = {
//初始化$
Init: function (selector, context) {
this.elements = [];
if (typeof selector === "function") {
this.elements.push(document);
this.ready(selector);
} else {
var context = context || document;
var isDocument = (ele) =>
Object.prototype.toString.call(ele) == "[object HTMLDocument]" ||
"[object Document]";
if (isDocument(selector)) {
this.elements.push(selector);
} else {
if (context.querySelectorAll) {
var arr = context.querySelectorAll(selector);
for (var i = 0; i < arr.length; i++) {
this.elements.push(arr[i]);
}
}
}
}
},
//实现each
each: function (callback) {},
//实现ready
ready: function (callback) {},
//实现bind
bind: function (type, callback) {},
};
_$.prototype.Init.prototype = _$.prototype;
window.$ = _$;
})(window || global);
ready
//实现ready
ready: function (callback) {
var isDocument = (ele) => Object.prototype.toString.call(ele) == '[object HTMLDocument]' || '[object Document]'
//如果已经取得了节点
if (isDocument(this.elements[0])) {
if (document.addEventListener) { //判断火狐、谷歌
document.addEventListener('DOMContentLoaded', function () {
document.removeEventListener('DOMContentLoaded', arguments.callee, false)
callback()
}, false)
} else if (document.attachEvent) { //判断IE
document.attachEvent('onreadystatechange', function () {
if (document.readyState == 'complete') {
document.detachEvent('onreadystatechange', arguments.callee);
callback()
}
})
} else if (document.lastChild == document.body) { //body已经加载完了,就直接回调了
callback()
}
}
},
each
//实现each
each: function (callback) {
if (this.elements.length > 0) {
for (var i = 0; i < this.elements.length; i++) {
callback.call(this, this.elements[i], i);
}
}
},
bind
//实现bind
bind: function (type, callback) {
if (document.addEventListener) { //判断火狐、谷歌
this.each(function (item, i) {
item.addEventListener(type, callback, false)
})
} else if (document.attachEvent) { //判断IE
this.each(function (item, i) {
item.attachEvent('on' + type, callback)
})
} else {
this.each(function (item, i) { //其他浏览器 egg: item.onclick = function(){}
item['on' + type] = callback
})
}
}
$.fn.extend/$.extend
$.fn.extend
是为查询的节点对象扩展方法,是基于$
的原型扩展的方法
$.extend
是扩展常规方法,是$的静态方法
官方给出解释:
jQuery.extend()
: Merge the contents of two or more objects together into the first object.(把两个或者更多的对象合并到第一个当中)
jQuery.fn.extend()
:Merge the contents of an object onto the jQuery prototype to provide new jQuery instance methods.(把对象挂载到jQuery
的prototype
属性,来扩展一个新的jQuery
实例方法)
$.fn.extend
方法的初衷是我们扩展之后可以用$("").newMetod()
这样访问,实际上就是给$
原型加一个extend
方法。这中间的fn
其实类似于命名空间的作用,没什么实际的意义。为的是和$.extend
作区分
$.fn.extend
; (function (win) {
...
_$.prototype.Init.prototype = _$.prototype;
_$.fn = _$.prototype; //把对象挂载到jQuery的prototype属性
var isObj = (o) => Object.prototype.toString().call(o) === '[object Object]';
$.fn.extend = function (obj) {
if (isObj(obj)) {
for (var i in obj) {
this[i] = obj //注意这里的this指向是 $.prototype
}
}
}
$.extend
var isObj = (o) => Object.prototype.toString().call(o) === '[object Object]';
...
_$.extend = function (obj) {
if (isObj(obj)) {
for (var i in obj) {
this[i] = obj[i]; //注意这里的this指向是 $
}
}
}
这俩看上去一模一样啊,没啥区别,注释里面已经说了,this
指向不同。咱们来看个例子:
<!DOCTYPE html>
<html>
<head>
<title>jQuery.extend()与jQuery.fn.extend()区别</title>
<meta charset="utf-8" />
<script type="text/javascript" src="jquery.js"></script>
<!-- 开始扩展 -->
<script type="text/javascript">
(function ($) {
$.extend({
sayHello: function () {
console.log("Hello");
},
});
$.fn.extend({
sayHello: function () {
console.log("Hello");
},
});
})(jQuery);
</script>
<!-- 调用 -->
<script type="text/javascript">
$(document).ready(function () {
//$.extend扩展调用
$.sayHello();
//$.fn.extend扩展调用
$("#test").sayHello();
});
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
这样以来就看的很明白了。jQuery.extend(object);
为扩展jQuery
类本身,为自身添加新的方法。$.xxx()
jQuery.fn.extend(object);
给jQuery
对象添加方法$('#test').xxx()
$.extend
常见用法
//在jquery全局对象中扩展一个net命名空间。
$.extend({ net: {} });
//方法扩展到之前扩展的Jquery的net命名空间中去。
$.extend($.net, {
sayHello: function () {
console.log("Hello");
},
});
//extend方法还有一个重载原型
//extend(boolean,dest,src1,src2,src3...),第一个参数boolean代表是否进行深度拷贝
var a = { protocol: "http", hash: { a: 1, b: 2 } };
var b = { host: "chuchur.com", hash: { b: 1, c: 2 } };
var result = $.extend(true, {}, a, b);
console.log(result); //{ protocol: 'http',host: 'chuchur.com', hash: { a: 1, b: 1,c:2 } }
var result = $.extend(false, {}, a, b);
console.log(result); //{ protocol: 'http',host: 'chuchur.com', hash: { b: 1, c:2 } }
完整代码
(function (win) {
var _$ = function (selector, context) {
return new _$.prototype.Init(selector, context);
};
_$.prototype = {
//初始化$
Init: function (selector, context) {
this.elements = [];
if (typeof selector === "function") {
this.elements.push(document);
this.ready(selector);
} else {
var context = context || document;
var isDocument = (ele) =>
Object.prototype.toString.call(ele) == "[object HTMLDocument]" ||
"[object Document]";
if (isDocument(selector)) {
this.elements.push(selector);
} else {
if (context.querySelectorAll) {
var arr = context.querySelectorAll(selector);
for (var i = 0; i < arr.length; i++) {
this.elements.push(arr[i]);
}
}
}
}
},
//实现each
each: function (callback) {
if (this.elements.length > 0) {
for (var i = 0; i < this.elements.length; i++) {
callback.call(this, this.elements[i], i);
}
}
},
//实现ready
ready: function (callback) {
var isDocument = (ele) =>
Object.prototype.toString.call(ele) == "[object HTMLDocument]" ||
"[object Document]";
//如果已经取得了节点
if (isDocument(this.elements[0])) {
if (document.addEventListener) {
//判断火狐、谷歌
document.addEventListener(
"DOMContentLoaded",
function () {
document.removeEventListener(
"DOMContentLoaded",
arguments.callee,
false
);
callback();
},
false
);
} else if (document.attachEvent) {
//判断IE
document.attachEvent("onreadystatechange", function () {
if (document.readyState == "complete") {
document.detachEvent("onreadystatechange", arguments.callee);
callback();
}
});
} else if (document.lastChild == document.body) {
//body已经加载完了,就直接回调了
callback();
}
}
},
//实现bind
bind: function (type, callback) {
if (document.addEventListener) {
//判断火狐、谷歌
this.each(function (item, i) {
item.addEventListener(type, callback, false);
});
} else if (document.attachEvent) {
//判断IE
this.each(function (item, i) {
item.attachEvent("on" + type, callback);
});
} else {
this.each(function (item, i) {
//其他浏览器 egg: item.onclick = function(){}
item["on" + type] = callback;
});
}
},
};
_$.prototype.Init.prototype = _$.prototype;
var isObj = (o) => Object.prototype.toString().call(o) === "[object Object]";
$.fn.extend = function (obj) {
if (isObj(obj)) {
for (var i in obj) {
this[i] = obj; //注意这里的this指向是 $.prototype
}
}
//....这里是简写
};
_$.extend = function (obj) {
if (isObj(obj)) {
for (var i in obj) {
this[i] = obj[i]; //注意这里的this指向是 $
}
}
//....这里是简写
};
window.$ = _$;
})(window || global);
到此,关于“JavaScript下jquery的使用方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!