如何为WordPress按钮添加打开JS模态框的onclick动作
解决方案:将自定义模态框转为WordPress按钮的onclick动作
问题背景
已将模态框的CSS、HTML、JS整合为单文件并通过WP Code Snippets插件注入网站头部,模态框可正常运行。现需利用WordPress主题支持的按钮JavaScript弹窗功能,将模态框绑定为按钮的onclick触发动作。
具体步骤
1. 调整JavaScript代码,让openModal函数全局可访问
原代码中openModal是局部函数,WordPress按钮的onclick无法调用,需修改为全局函数:
// 替换原openModal函数定义 function openModal() { modal.classList.remove("hidden"); overlay.classList.remove("hidden"); }
或者保留原函数表达式,添加一行挂载到全局:
// 在原openModal函数定义后添加 window.openModal = openModal;
2. 清理冗余代码
移除原代码中自带的触发按钮及对应事件监听:
- 删除HTML里的
<button class="btn btn-open">Open Modal</button> - 删除JS里的
openModalBtn.addEventListener("click", openModal); - 删除
const openModalBtn = document.querySelector(".btn-open");这行变量定义
3. 在WordPress主题按钮中设置onclick动作
找到主题按钮的「onclick动作链接」设置项,填入:
openModal()
修改后的完整代码(用于WP Code Snippets头部注入)
<section class="modal hidden"> <div class="flex"> <button class="btn-close">⨉</button> </div> <input type="button" onClick="location.href='https:www.login.com'" value='Current Clients'> <input type="button" onClick="location.href='https://www.bookhere.com'" value='New Clients'> </section> <div class="overlay hidden"></div> <script> const modal = document.querySelector(".modal"); const overlay = document.querySelector(".overlay"); const closeModalBtn = document.querySelector(".btn-close"); // 关闭模态框函数 function closeModal() { modal.classList.add("hidden"); overlay.classList.add("hidden"); }; closeModalBtn.addEventListener("click", closeModal); overlay.addEventListener("click", closeModal); // ESC键关闭模态框 document.addEventListener("keydown", function(e) { if (e.key === "Escape" && !modal.classList.contains("hidden")) { closeModal(); } }); // 打开模态框函数(全局可访问) function openModal() { modal.classList.remove("hidden"); overlay.classList.remove("hidden"); }; </script> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Inter", sans-serif; } body { display: flex; flex-direction: column; align-items: center; justify-content: center; color: #222; position: relative; min-height: 100vh; background-color: #b3e6f4; } .modal { display: flex; flex-direction: column; justify-content: center; gap: 0.4rem; width: 450px; padding: 1.3rem; min-height: 250px; position: absolute; z-index: 2; top: 20%; background-color: #c8b490; border: 1px solid #c8b490; border-radius: 15px; } .modal .flex { display: flex; align-items: center; justify-content: space-between; } .modal input { padding: 0.7rem 1rem; border: 1px solid #ddd; border-radius: 5px; font-size: 1.0em; font-family: georgia; font-weight: 500; background-color: #e8e4db; } .modal input:hover { background-color: #e8cfaa; color: #e8e4db; } .modal p { font-size: 1.2rem; color: #777; margin: 0.4rem 0 0.2rem; } button { cursor: pointer; border: none; font-weight: 600; background-color: #625d5a } .btn-close { transform: translate(10px, -20px); padding: 0.5rem 0.7rem; background: #eee; border-radius: 50%; } .overlay { position: fixed; top: 0; bottom: 0; left: 0; right: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); backdrop-filter: blur(3px); z-index: 1; } .hidden { display: none; } </style>
内容的提问来源于stack exchange,提问作者goldenpath
相关产品推荐
相关产品推荐

