• 工厂模式


     1 <!DOCTYPE html>
     2 <html>
     3 
     4     <head>
     5         <meta charset="UTF-8">
     6         <title></title>
     7     </head>
     8 
     9     <body>
    10         <script>
    11         var DomFactory = (function(){
    12     this.Link = function(){
    13         this.appendTo = function(target){
    14             var ele = document.createElement("link");
    15             ele.url = this.url;
    16             target.appendChild(ele);                    
    17         }
    18     }
    19     this.Img = function(){
    20         this.appendTo = function(target){
    21             var ele = document.createElement("img");
    22             ele.src = this.src;
    23             target.appendChild(ele);                    
    24         }
    25     }
    26     this.Div = function(){
    27         this.appendTo = function(target){
    28             var ele = document.createElement("div");
    29             ele.style = "position:absolute;color:red";
    30             ele.innerHTML = this.text;
    31             target.appendChild(ele);
    32         }
    33     }
    34     this.Input = function(){
    35         this.appendTo = function(target){
    36             var ele = document.createElement("input");
    37             ele.type = this.type || "text";
    38             ele.value = this.value;
    39             target.appendChild(ele);
    40         }
    41     }
    42     this.create = function(type){
    43         return new this[type];
    44     }
    45     return this;
    46 })();
    47 window.onload = function(){
    48     var _input = DomFactory.create("Input");
    49     _input.value = "hello";
    50     _input.appendTo(document.body);
    51             
    52     var _div = DomFactory.create("Div");
    53     _div.text = "hehehe";
    54     _div.appendTo(document.body);
    55 }
    56 
    57         </script>
    58     </body>
    59 
    60 </html>
  • 相关阅读:
    jsp文件中charset和pageEncoding的区别
    如果jsp表单元素的值为空,如何避免null出现在页面上?
    C# 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序。
    正则表达式
    事件委托与键盘事件
    事件对象的兼容性
    作用域解析题
    事件冒泡与事件铺获的解析
    浏览器内核
    js中级总结
  • 原文地址:https://www.cnblogs.com/thestudy/p/6285350.html
Copyright © 2020-2023  润新知