2019-06-18 13:17:59 1203浏览
今天千锋扣丁学堂HTML5培训老师给大家分享一篇关于jquery中为什么能用$操作的详细介绍,首先详细大家对jq对dom节点的操作都很熟悉了吧。直接用$来获取dom节点的方式也非常便捷方便,那么他是怎么实现的呢?
function Dom(selector){ this.dom = document.querySelector(selector); this.val = function (content) { if(content){ this.dom.value = content }else{ return this.dom.value; } } } function $(selector) { return new Dom(selector); } $("input").val("value")
(function(window, undefined) { jQuery = function(selector, context) { return new jQuery.fn.init(selector, context); } jQuery.fn = jQuery.prototype = { init: function(selector, context) { this.dom = document.querySelector(selector) }, val: function(content) { if(content) { this.dom.value = content } else { return this.dom.value; } } } jQuery.fn.init.prototype = jQuery.fn; window.$ = jQuery; })(window); $("input").val("value")
【关注微信公众号获取更多学习资料】 【扫码进入HTML5前端开发VIP免费公开课】