In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要讲解了"怎么写一款属于自己的JS类库",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么写一款属于自己的JS类库"吧!
API介绍和效果展示
事件绑定 Xuery.on(eventName, fn) 案例如下:
Xuery('#demo').on('click', function(e){ alert('hello world!') })
访问和设置css Xuery.css(string|object, ?[string]) 案例如下:
// 访问css Xuery('#demo').css('width') // 设置css Xuery('#demo').css('width', '1024px') // 设置css Xuery('#demo').css({ width: '1024px', height: '1024px' })
访问和设置属性 Xuery.attr(string|object, ?[string]) 案例如下:
// 访问attr Xuery('#demo').attr('title') // 设置attr Xuery('#demo').attr('title', '1024px') // 设置attrs Xuery('#demo').attr({ title: '1024px', name: '1024px' })
访问和设置html 案例如下:
// 访问 Xuery('#demo').html() // 设置 Xuery('#demo').html('前端学习原生框架')
还有其他几个常用的API在这里就不介绍了,大家可以在我的github上查看,或者基于这套基础框架,去扩展属于自己的js框架。
核心源码
以下源码相关功能我做了注释,建议大家认真阅读,涉及到原型链和构造函数的指向的问题,是实现上述调用方式的核心,又不懂可以在评论区交流沟通。
/** * 链模式实现自己的js类库 */ (function(win, doc){ var Xuery = function(selector, context) { return new Xuery.fn.init(selector, context) }; Xuery.fn = Xuery.prototype = { constructor: Xuery, init: function(selector, context) { // 设置元素长度 this.length = 0; // 默认获取元素的上下文document context = context || document; // id选择符,则按位非将-1转化为0 if(~selector.indexOf('#')) { this[0] = document.getElementById(selector.slice(1)); this.length = 1; }else{ // 在上下文中选择元素 var doms = context.getElementsByTagName(selector), i = 0, len = doms.length; for(; i=0;i--){ this[i].addEvent('on'+type, fn) } return this } // 不支持dom2的浏览器 }else{ return function(type, fn){ var i = this.length -1; for(; i>=0;i--){ this[i]['on'+type] = fn; } return this } } })() }) // 将‘-’分割线转换为驼峰式 Xuery.extend({ camelCase: function(str){ return str.replace(/\-(\w)/g, function(all, letter){ return letter.toUpperCase(); }) } }) // 设置css Xuery.extend({ css: function(){ var arg = arguments, len = arg.length; if(this.length
< 1){ return this } if(len === 1) { if(typeof arg[0] === 'string') { if(this[0].currentStyle){ return this[0].currentStyle[arg[0]]; }else{ return getComputedStyle(this[0], false)[arg[0]] } }else if(typeof arg[0] === 'object'){ for(var i in arg[0]){ for(var j=this.length -1; j>=0; j--){ this[j].style[Xuery.camelCase(i)] = arg[0][i]; } } } }else if(len === 2){ for(var j=this.length -1; j>=0; j--){ this[j].style[Xuery.camelCase(arg[0])] = arg[1]; } } return this } }) // 设置属性 Xuery.extend({ attr: function(){ var arg = arguments, len = arg.length; if(len = 0; j--){ this[j].setAttribute(i, arg[0][i]) } } } } else if(len === 2){ for(var j=this.length -1; j>=0; j--){ this[j].setAttribute(arg[0], arg[1]); } } return this } }) // 获取或者设置元素内容 Xuery.fn.extend({ html: function(){ var arg = arguments, len = arg.length; if(len === 0){ return this[0] && this[0][xss_clean] }else{ for(var i=this.length -1; i>=0; i--){ this[i][xss_clean] = arg[0]; } } return this } }) Xuery.fn.init.prototype = Xuery.fn; window.Xuery = Xuery; })(window, document);感谢各位的阅读,以上就是"怎么写一款属于自己的JS类库"的内容了,经过本文的学习后,相信大家对怎么写一款属于自己的JS类库这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.