Using JavaScript chain programming to simulate Jquery function

  • 2020-05-07 19:13:36
  • OfStack

The code is very simple, mainly to provide you with an idea, is also a long time to learn javascript a small practice.

Chain programming is the multiple operations (lines of code) through the dot "." link at 1 into a sentence of code. Chained code usually requires operations to have a return value, but for many operations it's mostly void, which returns nothing, which makes it hard to chain, and there are solutions, which may not be very elegant. The new idea of chain programming has become popular in jQuery


<span>Hello,World ! </span>
 <script type="text/javascript">
     Jq = function (tagName) {
         var tagArr = document.getElementsByTagName(tagName);
         var ret = {
             tag: tagArr,
             css: function (attribute, value) {
                 for (var i = 0; i < this.tag.length; i++) {
                     this.tag[i].style[attribute] = value;
                 }
                 return this;
             }
         }
         return ret;
     }
     window.onload = function () {
         Jq("span").css("color", "red")
                   .css("border", "1px solid green")
                   .css("padding", "10px");
     }
 </script>

If you want to ask, leave a message and make progress together


Related articles: