zoukankan      html  css  js  c++  java
  • 如何通过JS文件来渲染网页(即将html代码写在JS中,封装成一个模块,需要时调用):

    在动态渲染之前,需要在index.html中做好静态布局:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <link rel="stylesheet" href="./css/index.css">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" >
    </head>
    <body>
       <div class = "register_login">
           <!-- <div class = "register content">
                <div class="logo">
                        <img src="https://cas.1000phone.net/cas/images/login/logo.png">
                </div>
            <form class = "register-form">
                <div class="form-group">
                  <label for="exampleInputEmail1">用户名</label>
                  <input type="email" class="form-control register_username" id="register_username" placeholder="Username">
                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">密码</label>
                  <input type="password" class="form-control" id="register_password" placeholder="Password">
                </div>
                <button type="button" class="btn btn-success register_btn1" id = "register_btn1">去登陆</button>
                <button type="submit" class="btn btn-info register_btn" id = "register_btn">注册</button>
              </form>
           </div> -->
    
           <!-- <div class = "register content">
                <div class="logo">
                        <img src="https://cas.1000phone.net/cas/images/login/logo.png">
                </div>
            <form class = "register-form">
                <div class="form-group">
                  <label for="exampleInputEmail1">用户名</label>
                  <input type="email" class="form-control register_username" id="exampleInputEmail1" placeholder="Username">
                </div>
                <div class="form-group">
                  <label for="exampleInputPassword1">密码</label>
                  <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
                </div>
                <button type="button" class="btn btn-success register_btn1" id = "register_btn1">去注册</button>
                <button type="submit" class="btn btn-info register_btn">登录</button>
              </form>
           </div>
     -->
       </div> 
    </body>
    </html>
    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" ></script>
    <script src = "./js/page.js"></script>
    <script src = "./js/login.js"></script>
    <script src = "./js/register.js"></script>

    在page.js中创建page对象

    function Page() {
        this.container = $(".register_login");
        //console.log(this.container);
        this.flag = true;
        this.init();
       
    }
    
    
    Page.prototype = {
        init:function(){
            this.createContent();
        },
        createContent:function(params=this.flag){
            this.container.html('')
            if(params){
               
                this.login =  new Login(this.container);
            }else{
               
                this.register = new Register(this.container);
            }
        }
    }
    
    new Page();

    创建Login对象:

    //创建一个Login:
    function Login(container) {
        //容器实例属性:
        this.container = container;
        //调用入口方法:
        this.init();
    }
    
    //引入html标签:
    Login.template = `
     <div class = "register content">
        <div class="logo">
                <img src="https://cas.1000phone.net/cas/images/login/logo.png">
        </div>
        <form class = "register-form">
            <div class="form-group">
                <label for="exampleInputEmail1">用户名</label>
                <input type="email" class="form-control register_username" id="exampleInputEmail1" placeholder="Username">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">密码</label>
                <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
            </div>
            <button type="button" class="btn btn-success register_btn1" id = "register_btn1">去登陆</button>
            <button type="submit" class="btn btn-info register_btn">注册</button>
        </form>
    </div>`
    
    //Login的原型方法:
    Login.prototype = {
        init:function(){
        this.createDom();
        },
        createDom:function(){
            this.el = $("<div class='content'></div>");
            this.el.append(Login.template);
            console.log(Login.template)
            this.container.append(this.el);
        }
    }
    
    new Page();

    创建Register对象:

    //创建一个Register:
    function Register(container) {
        //容器实例属性:
        this.container = container;
        //调用入口方法:
        this.init();
    }
    
    //引入html标签:
    Register.template = `
     <div class = "register content">
        <div class="logo">
                <img src="https://cas.1000phone.net/cas/images/login/logo.png">
        </div>
        <form class = "register-form">
            <div class="form-group">
                <label for="exampleInputEmail1">用户名</label>
                <input type="email" class="form-control register_username" id="exampleInputEmail1" placeholder="Username">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">密码</label>
                <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
            </div>
            <button type="button" class="btn btn-success register_btn1" id = "register_btn1">去登陆</button>
            <button type="submit" class="btn btn-info register_btn">注册</button>
        </form>
    </div>`
    
    //Register的原型方法:
    Register.prototype = {
        init:function(){
        this.createDom();
        },
        createDom:function(){
            this.el = $("<div class='content'></div>");
            this.el.append(Register.template);
            console.log(Register.template)
            this.container.append(this.el);
        }
    }
    
    new Page();
  • 相关阅读:
    lvs持久连接及防火墙标记实现多端口绑定服务
    LVS负载均衡器DR模型的实现
    CentOS 6.5结合busybox完成自制Linux系统及远程登录和nginx安装测试
    CentOS 6.5下的lamp环境rsyslog+MySQL+loganalyzer实现日志集中分析管理
    centos中selinux功能及常用服务配置
    网站遭遇CC及DDOS攻击紧急处理方案
    centos6.5下系统编译定制iptables防火墙扩展layer7应用层访问控制功能及应用限制QQ2016上网
    centos 6.5内核编译步骤及配置详解
    centos系统初始化流程及实现系统裁剪
    iptables实现网络防火墙及地址转换
  • 原文地址:https://www.cnblogs.com/kinoko-1009/p/10519095.html
Copyright © 2011-2022 走看看