window.confirm詳解

一、window.confirm未定義

在使用window.confirm函數時,如果控制台輸出「window.confirm is not a function」,有可能是因為該函數被重寫了或者被刪除了,此時需要確認是否有其他代碼在頁面中重寫或者刪除了該函數。

可以使用以下方法來檢測window.confirm函數是否存在:

if(typeof window.confirm !== 'function'){
    alert('window.confirm未定義!');
}

二、window.confirm與confirm區別

confirm是JavaScript內置的函數,而window.confirm是瀏覽器提供的全局對象中的一個方法。

二者在功能上沒有區別,都是彈出一個對話框,提示用戶是否繼續操作。但是,window.confirm函數只能在瀏覽器中使用,而confirm函數可以在不同的環境中使用,如Node.js等。

以下是window.confirm和confirm的使用示例:

window.confirm("確定要刪除此項嗎?");

confirm("此操作不可撤銷,是否繼續?");

三、阻止window.confirm函數執行

可以通過在window.confirm函數前加return false來阻止window.confirm函數的執行,從而阻止默認的對話框彈出。

以下是使用return false阻止window.confirm函數的執行的示例:

function onDelete(){
    if(!confirm("確定要刪除此項嗎?")){
        return false;
    }
    //執行刪除操作...
}

四、自定義window.confirm對話框

可以使用CSS和JavaScript自定義window.confirm的樣式。

HTML部分:

<button onclick="showCustomConfirm()">自定義window.confirm</button>

CSS部分:

/*彈出框樣式*/
.custom-confirm-box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 300px;
    padding: 20px;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 0 10px #ddd;
    border-radius: 4px;
    z-index: 999;
}
/*按鈕樣式*/
.custom-confirm-button {
    width: 100px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    background-color: #f00;
    color: #fff;
    border-radius: 4px;
    cursor: pointer;
}
.custom-confirm-button:hover {
    background-color: #f60;
}

JavaScript部分:

function showCustomConfirm(){
    //創建遮罩層
    var mask = document.createElement('div');
    mask.style.position = 'fixed';
    mask.style.top = '0';
    mask.style.left = '0';
    mask.style.width = '100%';
    mask.style.height = '100%';
    mask.style.backgroundColor = 'rgba(0,0,0,0.4)';
    document.body.appendChild(mask);

    //創建彈出框
    var box = document.createElement('div');
    box.className = 'custom-confirm-box';
    box.innerHTML = '<p>自定義window.confirm</p><p>確定要刪除此項嗎?</p><div><button class="custom-confirm-button">確定</button><button class="custom-confirm-button">取消</button></div>';
    document.body.appendChild(box);

    //綁定事件
    var buttons = box.querySelectorAll('.custom-confirm-button');
    buttons[0].onclick = function(){
        //點擊確定按鈕後執行的操作
        mask.remove();
        box.remove();
    };
    buttons[1].onclick = function(){
        //點擊取消按鈕後執行的操作
        mask.remove();
        box.remove();
    }
}

五、兼容IE6-IE7的window.confirm方法

在IE6-IE7中,window.confirm默認樣式與彈出框樣式相同,並且無法自定義,同時,由於IE6-IE7中沒有window.confirm方法,所以需要手動模擬

以下是兼容IE6-IE7的window.confirm方法的示例:

//定義window.confirm方法
if (!window.confirm) {
    window.confirm = function (msg, title) {
        title = title || '提示';
        //模擬彈出框
        var box = document.createElement('div');
        box.style.position = 'absolute';
        box.style.top = '50%';
        box.style.left = '50%';
        box.style.transform = 'translate(-50%,-50%)';
        box.style.width = '300px';
        box.style.padding = '20px';
        box.style.backgroundColor = '#fff';
        box.style.border = '1px solid #ccc';
        box.style.boxShadow = '0 0 10px #ddd';
        box.style.borderRadius = '4px';
        box.style.zIndex = '999';

        //標題部分
        var titleBox = document.createElement('div');
        titleBox.style.height = '30px';
        var titleSpan = document.createElement('span');
        titleSpan.style.float = 'left';
        titleSpan.style.lineHeight = '30px';
        titleSpan.style.textIndent = '10px';
        titleSpan.innerHTML = title;
        var closeSpan = document.createElement('span');
        closeSpan.style.float = 'right';
        closeSpan.style.lineHeight = '30px';
        closeSpan.style.textAlign = 'center';
        closeSpan.style.width = '30px';
        closeSpan.style.cursor = 'pointer';
        closeSpan.innerHTML = '×';
        closeSpan.onclick = function () {
            document.body.removeChild(mask);
            document.body.removeChild(box);
            return false;
        };
        titleBox.appendChild(titleSpan);
        titleBox.appendChild(closeSpan);

        //內容部分
        var msgBox = document.createElement('div');
        msgBox.style.padding = '10px';
        msgBox.style.borderTop = '1px solid #ddd';
        msgBox.style.borderBottom = '1px solid #ddd';
        msgBox.innerHTML = msg;

        //按鈕部分
        var btnBox = document.createElement('div');
        btnBox.style.height = '50px';
        btnBox.style.lineHeight = '50px';
        btnBox.style.textAlign = 'center';
        var okBtn = document.createElement('span');
        okBtn.style.display = 'inline-block';
        okBtn.style.width = '100px';
        okBtn.style.height = '30px';
        okBtn.style.lineHeight = '30px';
        okBtn.style.backgroundColor = '#f00';
        okBtn.style.color = '#fff';
        okBtn.style.borderRadius = '4px';
        okBtn.style.marginRight = '30px';
        okBtn.style.cursor = 'pointer';
        okBtn.innerHTML = '確定';
        okBtn.onclick = function () {
            document.body.removeChild(mask);
            document.body.removeChild(box);
            return true;
        };
        var cancelBtn = document.createElement('span');
        cancelBtn.style.display = 'inline-block';
        cancelBtn.style.width = '100px';
        cancelBtn.style.height = '30px';
        cancelBtn.style.lineHeight = '30px';
        cancelBtn.style.backgroundColor = '#ccc';
        cancelBtn.style.color = '#fff';
        cancelBtn.style.borderRadius = '4px';
        cancelBtn.style.cursor = 'pointer';
        cancelBtn.innerHTML = '取消';
        cancelBtn.onclick = function () {
            document.body.removeChild(mask);
            document.body.removeChild(box);
            return false;
        };
        btnBox.appendChild(okBtn);
        btnBox.appendChild(cancelBtn);

        box.appendChild(titleBox);
        box.appendChild(msgBox);
        box.appendChild(btnBox);

        var mask = document.createElement('div');
        mask.style.position = 'fixed';
        mask.style.top = '0';
        mask.style.left = '0';
        mask.style.width = '100%';
        mask.style.height = '100%';
        mask.style.backgroundColor = 'rgba(0,0,0,0.4)';
        mask.style.zIndex = '998';

        document.body.appendChild(mask);
        document.body.appendChild(box);

        return false;
    };
}

原創文章,作者:CYEA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/135618.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
CYEA的頭像CYEA
上一篇 2024-10-04 00:14
下一篇 2024-10-04 00:14

相關推薦

  • 神經網路代碼詳解

    神經網路作為一種人工智慧技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網路的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網路模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁碟中。在執行sync之前,所有的文件系統更新將不會立即寫入磁碟,而是先緩存在內存…

    編程 2025-04-25
  • git config user.name的詳解

    一、為什麼要使用git config user.name? git是一個非常流行的分散式版本控制系統,很多程序員都會用到它。在使用git commit提交代碼時,需要記錄commi…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25
  • Python輸入輸出詳解

    一、文件讀寫 Python中文件的讀寫操作是必不可少的基本技能之一。讀寫文件分別使用open()函數中的’r’和’w’參數,讀取文件…

    編程 2025-04-25
  • 詳解eclipse設置

    一、安裝與基礎設置 1、下載eclipse並進行安裝。 2、打開eclipse,選擇對應的工作空間路徑。 File -> Switch Workspace -> [選擇…

    編程 2025-04-25
  • Python安裝OS庫詳解

    一、OS簡介 OS庫是Python標準庫的一部分,它提供了跨平台的操作系統功能,使得Python可以進行文件操作、進程管理、環境變數讀取等系統級操作。 OS庫中包含了大量的文件和目…

    編程 2025-04-25
  • nginx與apache應用開發詳解

    一、概述 nginx和apache都是常見的web伺服器。nginx是一個高性能的反向代理web伺服器,將負載均衡和緩存集成在了一起,可以動靜分離。apache是一個可擴展的web…

    編程 2025-04-25
  • MPU6050工作原理詳解

    一、什麼是MPU6050 MPU6050是一種六軸慣性感測器,能夠同時測量加速度和角速度。它由三個感測器組成:一個三軸加速度計和一個三軸陀螺儀。這個組合提供了非常精細的姿態解算,其…

    編程 2025-04-25

發表回復

登錄後才能評論