最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript 自定義彈出窗口的實現(xiàn)代碼

 更新時間:2023年09月28日 15:57:41   作者:梁云亮  
這篇文章主要介紹了JavaScript 自定義彈出窗口的實現(xiàn)代碼,實現(xiàn)一采用html編寫彈出窗口內(nèi)容,實現(xiàn)二采用JavaScript編寫彈出窗口內(nèi)容,結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

最終效果

單擊彈出窗口

在這里插入圖片描述

單擊確定按鈕

在這里插入圖片描述

單擊取消按鈕,最初彈出的窗口隱藏

實現(xiàn)一:采用html編寫彈出窗口內(nèi)容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #dialog {
            position: absolute;
            z-index: 9999;
            top: 110px;
            left: 45%;
            transform: translate(-50%, -50%);
            width: 290px;
            height: 130px;
            background-color: #fff;
            border: 1px solid #ccc;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
            border-radius: 5px;
            padding: 20px;
            display: none;
        }
        #dialog h3 {
            margin-top: 0;
        }
        #dialog div{
            margin-top: 15px;
        }
        #dialog button {
            margin-right: 10px;
            float: right;
        }
    </style>
</head>
<body>
<button onclick="showDialog()">彈出窗口</button>
<div id="dialog">
    <h3>導(dǎo)出數(shù)據(jù)</h3>
    <p>按頁面條件導(dǎo)出數(shù)據(jù)還是導(dǎo)出所有的數(shù)據(jù)</p>
    <input type="radio" name="type" value="1" checked> 按頁面條件導(dǎo)出
    <input type="radio" name="type" value="2"> 導(dǎo)出全部
    <div>
        <button onclick="hide()">取消</button>
        <button onclick="javascript:alert(22);">確定</button>
    </div>
</div>
<script>
    function show() {
        // 獲取對話框元素并設(shè)置標(biāo)題和消息
        let dialog = document.querySelector('#dialog');
        // 顯示對話框
        dialog.style.display = 'block';
    }
    function hide() {
        // 獲取對話框元素并隱藏
        let dialog = document.querySelector('#dialog');
        dialog.style.display = 'none';
    }
    function showDialog() {
        let dialog = document.querySelector('#dialog');
        dialog.querySelector('button').style.display = 'block';
        show('確認(rèn)刪除', '你確定要刪除這條記錄嗎?');
    }
</script>
</body>
</html>

實現(xiàn)二:采用JavaScript編寫彈出窗口內(nèi)容

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #dialog {
            position: absolute;
            z-index: 9999;
            top: 110px;
            left: 45%;
            transform: translate(-50%, -50%);
            width: 290px;
            height: 130px;
            background-color: #fff;
            border: 1px solid #ccc;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
            border-radius: 5px;
            padding: 20px;
            display: none;
        }
        #dialog h3 {
            margin-top: 0;
        }
        #dialog div {
            margin-top: 15px;
        }
        #dialog button {
            margin-right: 10px;
            float: right;
        }
    </style>
</head>
<body>
<button onclick="showDialog('導(dǎo)出數(shù)據(jù)')">彈出窗口</button>
<script>
    function showDialog() {
        let dialogDiv = document.createElement("div");
        dialogDiv.id = "dialog";
        let h3Element = document.createElement("h3");
        h3Element.innerText = '導(dǎo)出數(shù)據(jù)';
        dialogDiv.appendChild(h3Element);
        let pElement = document.createElement("p");
        pElement.innerText = "按頁面條件導(dǎo)出數(shù)據(jù)還是導(dǎo)出所有的數(shù)據(jù)";
        dialogDiv.appendChild(pElement);
        let div1 = document.createElement("div");
        let input1Element = document.createElement("input");
        input1Element.type = "radio";
        input1Element.name = "requestType";
        input1Element.checked=true;
        input1Element.value = 1;
        div1.appendChild(input1Element);
        let span1 = document.createElement("span");
        span1.innerText = "按頁面條件導(dǎo)出";
        div1.appendChild(span1);
        let input2Element = document.createElement("input");
        input2Element.type = "radio";
        input2Element.name = "requestType";
        input2Element.value = 2;
        div1.appendChild(input2Element);
        let span2 = document.createElement("span");
        span2.innerText = "按頁面條件導(dǎo)出";
        div1.appendChild(span2);
        dialogDiv.appendChild(div1);
        let div2 = document.createElement("div");
        let button1 = document.createElement("button");
        button1.addEventListener("click", function () {
            dialogDiv.style.display = 'none';
        });
        button1.innerText = "取消";
        div2.appendChild(button1);
        let button2 = document.createElement("button");
        button2.addEventListener("click", function () {
            let checkValue=1;
            let radioButtons = document.getElementsByName('requestType');
            for (let i = 0; i < radioButtons.length; i++) {
                if (radioButtons[i].checked) {
                    checkValue =  radioButtons[i].value;
                    break;
                }
            }
            alert(checkValue)
        });
        button2.innerText = "確定";
        div2.appendChild(button2);
        dialogDiv.appendChild(div2);
        document.body.appendChild(dialogDiv);
        // 顯示對話框
        dialogDiv.style.display = 'block';
    }
</script>
</body>
</html>

實現(xiàn)三:抽取成獨(dú)立的JS插件,在網(wǎng)頁中使用

第一步:抽取出exportDialog.css:

#dialog {
    position: absolute;
    z-index: 9999;
    top: 110px;
    left: 45%;
    transform: translate(-50%, -50%);
    width: 290px;
    height: 130px;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
    border-radius: 5px;
    padding: 20px;
    display: none;
}
#dialog h3 {
    margin-top: 0;
}
#dialog div {
    margin-top: 15px;
}
#dialog button {
    margin-right: 10px;
    float: right;
}

第二步;抽取出exportDialog.js:

/**
 *
 * @param url1  按頁面條件導(dǎo)出
 * @param url2  導(dǎo)出全部數(shù)據(jù)
 */
function showDialog(url1,url2 ) {
    let dialogDiv = document.createElement("div");
    dialogDiv.id = "dialog";
    let h3Element = document.createElement("h3");
    h3Element.innerText = '導(dǎo)出數(shù)據(jù)';
    dialogDiv.appendChild(h3Element);
    let pElement = document.createElement("p");
    pElement.innerText = "按頁面條件導(dǎo)出數(shù)據(jù)還是導(dǎo)出所有的數(shù)據(jù)";
    dialogDiv.appendChild(pElement);
    let div1 = document.createElement("div");
    let input1Element = document.createElement("input");
    input1Element.type = "radio";
    input1Element.name = "requestType";
    input1Element.checked=true;
    input1Element.value = 1;
    div1.appendChild(input1Element);
    let span1 = document.createElement("span");
    span1.innerText = "按頁面條件導(dǎo)出";
    div1.appendChild(span1);
    let input2Element = document.createElement("input");
    input2Element.type = "radio";
    input2Element.name = "requestType";
    input2Element.value = 2;
    div1.appendChild(input2Element);
    let span2 = document.createElement("span");
    span2.innerText = "全部導(dǎo)出";
    div1.appendChild(span2);
    dialogDiv.appendChild(div1);
    let div2 = document.createElement("div");
    let button1 = document.createElement("button");
    button1.addEventListener("click", function () {
        dialogDiv.style.display = 'none';
    });
    button1.innerText = "取消";
    div2.appendChild(button1);
    let button2 = document.createElement("button");
    button2.addEventListener("click",function () {
        let checkValue=1;
        let radioButtons = document.getElementsByName('requestType');
        for (let i = 0; i < radioButtons.length; i++) {
            if (radioButtons[i].checked) {
                checkValue =  radioButtons[i].value;
                break;
            }
        }
        if(checkValue==1){
            //按頁面條件導(dǎo)出
            document.location.href= url1
        }
        if(checkValue==2){
            //全部導(dǎo)出
            document.location.href= url2
        }
        //隱藏對話框
        dialogDiv.style.display = 'none';
    } );
    button2.innerText = "確定";
    div2.appendChild(button2);
    dialogDiv.appendChild(div2);
    document.body.appendChild(dialogDiv);
    // 顯示對話框
    dialogDiv.style.display = 'block';
}

第三步:在頁面中使用:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/tiku/static/dialog/exportDialog.css" rel="external nofollow" ></link>
    <script src="/tiku/static/dialog/exportDialog.js"></script>
</head>
<body>
<button onclick="showDialog('/tiku/subject/export?state=1&pid=-2','/tiku/subject/export?pid=-2')">彈出窗口</button>
<script>
</script>
</body>
</html>

到此這篇關(guān)于JavaScript 自定義彈出窗口的文章就介紹到這了,更多相關(guān)js自定義彈出窗口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

安远县| 通许县| 息烽县| 邵武市| 金乡县| 裕民县| 区。| 溧阳市| 浏阳市| 芦溪县| 论坛| 昌黎县| 苏尼特左旗| 左贡县| 竹山县| 五指山市| 睢宁县| 师宗县| 青阳县| 巩留县| 夏津县| 巴林右旗| 永胜县| 西乌| 金华市| 塔城市| 明水县| 永顺县| 汝州市| 双桥区| 甘德县| 抚州市| 莲花县| 灵石县| 兴和县| 吉林省| 高淳县| 休宁县| 厦门市| 贺州市| 丰宁|