前端 自定义确认提示框
唐宋元明清2188 人气:1js默认提示框
js有三种默认提示框
提示框 alert('hello world!');
确认框 var result=confirm('确认删除XX文件?');result为bool类型
回复确认框 var result=prompt('请输入文件标题:');result返回输入的值
自定义提示框
下文通过js自动生成一个自定义提示框
function showDialog(content) { var layer = document.createElement("div"); layer.id = "layer"; var style = { background: "#ccc", position: "absolute", zIndex: 10, width: "auto", height: "40px", left: "50%", top: "0%", marginLeft: "-100px", marginTop: "10px", padding: "4px 10px" } for (var i in style) { layer.style[i] = style[i]; } if (document.getElementById("layer") == null) { document.body.appendChild(layer); layer.innerHTML = content; layer.style.textAlign = "center"; layer.style.lineHeight = "40px"; setTimeout("document.body.removeChild(layer)", 3000) } }
效果如下:
自定义确认框
下文提供一个自定义提示框的案例,通过另一种方案(样式设置)来实现:
1 <div class="confirmWindow" id="confirmwindow"> 2 <div class="body"> 3 <div class="content">确认要删除XX文件么?</div> 4 <div class="btns"> 5 <a href="javascript:void(0);" class="btn-cancel" id="btn_cancel" onClick="cancelOnClick()">取消</a> 6 <a href="javascript:void(0);" class="btn-confirm" id="btn_ok" onClick="okOnClick()">确认</a> 7 </div> 8 </div> 9 </div>
通过js设置元素的display属性,来控制提示框的显示隐藏
document.getElementById("confirmwindow").style.display = "inline";
document.getElementById("confirmwindow").style.display = "none";
效果如下:
效果如下:
以上demo,完整案例请下载:example-MyconfirmDialog
或者访问github地址:https://github.com/Kybs0/Kybs0HtmlCssJsDemo/tree/masterhttps://img.qb5200.com/download-x/definedAlertWindow
参考文章:
- Html隐藏方式 https://blog.csdn.net/weixin_43846130/articlehttps://img.qb5200.com/download-x/details/95963426
- Flex布局 https://www.cnblogs.com/kybs0/articles/12205052.html
- 获取元素 https://blog.csdn.net/simon_xing/articlehttps://img.qb5200.com/download-x/details/81395854
加载全部内容