首页 > 代码库 > JavaScript链式编程模拟Jquery函数

JavaScript链式编程模拟Jquery函数

 1 <span>Hello,World!</span> 2 <script type="text/javascript"> 3     Jq = function (tagName) { 4         var tagArr = document.getElementsByTagName(tagName); 5         var ret = { 6             tag: tagArr, 7             css: function (attribute, value) { 8                 for (var i = 0; i < this.tag.length; i++) { 9                     this.tag[i].style[attribute] = value;10                 }11                 return this;12             }13         }14         return ret;15     }16 17     window.onload = function () {18         Jq("span").css("color", "red")19                   .css("border", "1px solid green")20                   .css("padding", "10px");21     }22 </script>

 

JavaScript链式编程模拟Jquery函数