JavaScript 自定義彈出窗口的實(shí)現(xiàn)代碼
最終效果
單擊彈出窗口
單擊確定按鈕
單擊取消按鈕,最初彈出的窗口隱藏
實(shí)現(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() { // 獲取對(duì)話框元素并設(shè)置標(biāo)題和消息 let dialog = document.querySelector('#dialog'); // 顯示對(duì)話框 dialog.style.display = 'block'; } function hide() { // 獲取對(duì)話框元素并隱藏 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)刪除', '你確定要?jiǎng)h除這條記錄嗎?'); } </script> </body> </html>
實(shí)現(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); // 顯示對(duì)話框 dialogDiv.style.display = 'block'; } </script> </body> </html>
實(shí)現(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 } //隱藏對(duì)話框 dialogDiv.style.display = 'none'; } ); button2.innerText = "確定"; div2.appendChild(button2); dialogDiv.appendChild(div2); document.body.appendChild(dialogDiv); // 顯示對(duì)話框 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Javascript 是你的高階函數(shù)(高級(jí)應(yīng)用)
這篇文章主要介紹了Javascript 是你的高階函數(shù) ,需要的朋友可以參考下2015-06-06淺談js中Object.create()與new的具體實(shí)現(xiàn)與區(qū)別
本文主要介紹了js中Object.create()與new的具體實(shí)現(xiàn)與區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03JavaScript實(shí)現(xiàn)多個(gè)物體同時(shí)運(yùn)動(dòng)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)多個(gè)物體同時(shí)運(yùn)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03js中document.getElementById(id)的具體用法
本文主要介紹了js中document.getElementById(id)的具體用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04如何使用JavaScript實(shí)現(xiàn)棧與隊(duì)列
這篇文章主要介紹了如何使用JavaScript實(shí)現(xiàn)棧與隊(duì)列。棧和隊(duì)列是web開發(fā)中最常用的兩種數(shù)據(jù)結(jié)構(gòu)。絕大多數(shù)用戶,甚至包括web開發(fā)人員,都不知道這個(gè)驚人的事實(shí)。,需要的朋友可以參考下2019-06-06用JavaScript 判斷用戶使用的是 IE6 還是 IE7
判斷IE瀏覽器的腳本,方便根據(jù)瀏覽器不懂,支持不同的代碼的分別調(diào)用。2008-01-01