zoukankan      html  css  js  c++  java
  • popup介绍

    一、作用

    用于使浏览器自动生成弹窗

    二、示例

    1、新建Django项目,新建APP:app01, 项目根目录下新建文件夹static

    2、静态文件配置,在settings.py中配置static:

    3、路由配置, urls.py:

    from django.contrib import admin
    from django.urls import path
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('index/', views.index),
        path('add/city/', views.add_city),
    ]
    View Code

    4、编写view函数,views.py:

    from django.shortcuts import render, HttpResponse
    
    
    def index(request):
        """首页"""
        return render(request, "index.html", locals())
    
    
    def add_city(request):
        """添加城市"""
    
        if request.method == "POST":
            # 获取数据  存入数据库
            # ........
            city_info = {"id": 5, "city": "成都"}
            return render(request, "pop-response.html", locals())  # 新建完成后自动关闭窗口
        return render(request, "add_city.html")
    View Code

    5、templates模板编写:

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <form>
        <p><input type="text"></p>
        <p>
            <select id="city">
                <option value=1>北京</option>
                <option value=2>天津</option>
                <option value=3>上海</option>
                <option value=4>深圳</option>
            </select>
            <input type="button" value="+" onclick="popUp();">
        </p>
    </form>
    
    <script>
        function func(cityId, cityTitle){
            //构造一个option标签
            var tag = document.createElement("option");
            tag.value = cityId;
            tag.innerText = cityTitle;
            //找到select框  将option添加进去
            var city = document.getElementById("city");
            city.appendChild(tag);
        }
    
        function popUp() {
            //打开新弹窗  (弹窗地址,弹窗名, 弹窗格式设置)
            window.open("/add/city/", "x1", "status=1, height=500, width=500, toolbar=0, resizeable=0");
    
        }
    </script>
    </body>
    </html>
    View Code

    add_city.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .box {
                 200px;
                height: 200px;
                border: 1px solid dodgerblue;
            }
        </style>
    </head>
    <body>
    <h3>新建城市</h3>
    <div class="box">
        <form method="post">
            {% csrf_token %}
            <input type="text" name="city">
            <input type="submit" value="提交">
        </form>
    </div>
    </body>
    </html>
    View Code

    pop-response.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>正在关闭</title>
    </head>
    <body>
    
    <script src="/static/commons.js"></script>
    {#引入文件,里面的'{{ id }}'不会被识别,就只是把它当做普通的字符串打印出来#}
    
    <script>
    
        //function close(){
            //window.close();
        //}
        //close();
    
        // 自执行函数   在写完函数后, 后面直接加括号便可以执行,括号内可以传递参数, 执行效果同上
        (function(){
            // 获取到是谁把这个页面打开的
            opener.func({{ id }}, "{{ city }}"); //传递参数 {"id": 5, "city": "成都"}
    
            window.close();
        })();
    
        // 可以传参:
        //(function(arg){
    
        //})("x1")  // arg=x1
    </script>
    </body>
    </html>
    View Code

    6、static文件夹下新建文件commons.js:

    alert('{{id}}');  //在pop-response.html中引入它
    View Code

    7、启动项目,在浏览器输入:http://127.0.0.1:8088/index/进入首页,点击按钮弹出新窗口,新增城市,提交后窗口自动关闭

  • 相关阅读:
    【原】泛型委托
    【原】web页面登陆验证
    【原】在一般处理程序中设置session
    16Aspx.com-PHP企业整站源码 景观石材大理石类织梦模板 含手机移动端 完整源码 APP+PC
    16Aspx.com-将15位身份证转换成18位
    16Aspx.com-书通网中小学生免费在线学习网站源码 带采集带手机版帝国cms内核
    16Aspx.com源码2014年7月详细
    Web电子商务网(三层)V2.0源码
    毫秒级百万数据分页存储过程
    C#做的一个加密/解密的类
  • 原文地址:https://www.cnblogs.com/yanlin-10/p/9671367.html
Copyright © 2011-2022 走看看