昨天做有个需求,要confirm窗口默认选中取消。不知道有什么简单的方法,就模拟了一个。
说实话现在真不想做这些锁碎的事情,无奈。。。
基本思路就是利用showModalDialog打开模拟的页面,在页面里点击“确定”,“取消”按钮返回不同的值。
然后在父页面中判断返回值。代码没有整理,直接贴出来做个参考。
模拟页面confirm.htm
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <title></title>
- <linkhref="../style/main_login.css"rel="stylesheet"type="text/css"/>
- <script>
- functionreturnModal(reg){
- window.returnValue=reg;
- window.close();
- }
- </script>
- </head>
- <bodystyle="BACKGROUND=#ECE9D8">
- <br>
- <imgsrc="../images/confirm.gif"style="vertical-align:middle">
- <script>
- varmsg=window.dialogArguments;
- document.write(msg);
- </script><br><br>
- <divalign="center">
- <inputtype="button"value="确定"onclick="returnModal(1)">
- <inputtype="button"value="取消"id="cancle"onclick="returnModal(0)">
- </div>
- <script>
- //虽然延时0秒,但可以保证focus在前面的按钮显示之后执行
- vartmptime=window.setTimeout("clear()",0);
- functionclear(){
- clearTimeout(tmptime);
- document.getElementById("cancle").focus();
- //document.getElementById("cancle").select();
- }
- </script>
- </body>
- </html>
调用的父窗口test.htm
- <script>
- functionmyconfirm(msg){
- varsFeatures="status:0;scroll:0;help:0;edge:raised;dialogHeight:"+8+";dialogWidth:"+16;
- varreturnValues=window.showModalDialog("common/confirm.htm",msg,sFeatures);
- if(typeofreturnValues=="undefined")returnValues=0;
- //alert(returnValues);
- returnreturnValues;
- }
- functiontest(){
- if(myconfirm("真的需要删除这条记录吗?"))
- alert("ok");
- }
- </script>
- <inputtype="button"onclick="test()"value="test">