求助!!!!!!
如题,有没有人做过网页上类似于alert的对话框!
是柱塞式的。
详细的情况,我给大家打个比方!!
function ex(){
   alert("系统阻塞式的alert");
   document.getElementById("username").value="帮帮我";
}
如上述例子,用系统定义的alert函数是阻塞式的! 必须关闭系统弹出的alert后,才会赋值!!现在想要实现的就是这种阻塞式的对话框。。
function ex2(){
   myAlert("自定义阻塞式的alert"); 
   document.getElementById("username").value="帮帮我";
}ps:已经写出模态的对话框!但是,他会在不关闭对话框的同时继续赋值!求高手帮忙,或给出代码,思路,皆可!!

解决方案 »

  1.   

    不太理解,可以试试settimeout 呢?
      

  2.   

    几个思路:
    1、你可以调整代码结果,让对话框关闭后,再执行赋值
    2、你可以用setInterval()不断检验对话框是否关闭,一旦关闭,再执行赋值
    var a = myAlert("自定义阻塞式的alert");  
    var timer = setInterval(function(){
      if(a.hasClose){
        document.getElementById("username").value="帮帮我";
        clearInterval(timer);
      }
    },500);
      

  3.   

    既然已经写出了模态对话框,那么给模态对话框上的确定按钮绑个onclick事件,比如这个确定按钮的id='sure',
    document.getElementById('sure').onclick = function(){
      document.getElementById("username").value="帮帮我";
    }
      

  4.   

    这个我知道。
    但是情况有些可能是这样的。。
    如:你要同时验证2个text文本域。。
    结果2个都验证通不过!
    你再验证的时候。不会一个一点的验证吧。肯定是放在一个function验证;也就是在同一段代码中验证。
    这种情况就会,弹出一个对话框,然后你点了确定,然后再弹出一个对话框。!你return了也会弹出来。因为代码已经执行了!
      

  5.   

    function showMyAlert(txt){
    dgStatu=false;
    myAlert(txt);//这里是我自己封装的alert
    var timer = setInterval(function(){
        if(dgStatu){
    alert("chenggong!");
        clearInterval(timer);
        }
    },10);//这里的意思是阻塞吗?会产生效果么?
    }function show2Alert(){
    showMyAlert('hello');
    showMyAlert('hello1';
    }上面代码是我测得7楼朋友的方法。用firebug调试。show2Alert这个方法中两个
    showMyAlert()方法都会执行。一次性弹出2个对话框
      

  6.   

    我觉得你们应该先搞懂setTimeout与setInterval的工作原理。。它并不会阻塞线程的,它们只是把函数放到线程的最末端而已。。
    而且楼主你想阻塞线程的做法是行不通的,因为根本没有API可以做到
      

  7.   

    那系统的alert函数,是怎么实现的呢!!
      

  8.   

    LZ,我想你要的效果其实是“关闭弹出的窗口后才进行后续操作” 是吧?
    其实alert函数的阻塞只是其中一种实现方法。你也可以自行模拟一种类似阻塞的行为,就和4楼5楼说的类似,
    例如你的要在关闭窗口后执行的代码可以写在一个函数里,关闭窗口后执行这个函数或这段代码就可以了。加上貌似你是要在验证页面后弹出窗口。那么验证代码也可以写成一个函数。如下:
    function validatin(){
       // Check all textbox valid or not. return true or false.   
    }function afterCloseWindow(){
       // Do something
    }//你的myAlert再增加一个参数,传入一个function,如下
    function myAlert(text, fn){
       // 你的代码,当点击确认或者关闭弹出窗口的时候就运行fn函数
    }//那你的ex2方法就变成了
    function ex2(){
      myAlert("自定义阻塞式的alert",afterCloseWindow);
    }
    //或者
    function ex2() {
       myAlert("自定义阻塞式的alert",function(){
           document.getElementById("username").value="帮帮我";
       })
    }你看这样行不行?
      

  9.   


    系统的alert是有它引擎的接口做的,也就是由源码实现,我们是没办法调用的
      

  10.   

    你脑筋就是有点转不过弯来。。算了,直接给你代码吧,自己慢慢琢磨吧。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
        <title> New Document </title>  
        <style>
         div{
         margin: 10px;
         }
         #tip{
         color: red;
         }
         .hide{
         display: none;
         }
         #myalert{
         position: absolute;
         top: 0;
         left: 0;
         z-index: 1000;
         width: 100%;
         height: 100%;
         background-red: gray;
         }
         #myalert div{
         width:200px;
         height: 100px;
         border: 1px solid blue;
         position: absolute;
         top:100px;
         left: 500px;
         }
         form.highlight{
         border: 1px solid red;
         }
        </style>
    </head>  
    <body>  
    <form>
    <div id="tip" class="hide">不能为空</div>
    <div>
        <input id="input_1" type="text" value=""/> 
        <font color="red"  class="hide">不能为空</font>
    </div>
    <div>
        <input id="input_2" type="text" value=""/> 
        <font color="red"  class="hide">不能为空</font>
    </div>
        <div>
        <input id="input_3" type="text" value=""/> 
        <font color="red"  class="hide">不能为空</font>
    </div>
    <div>
        <input id="input_4" type="text" value=""/> 
        <font color="red"  class="hide">不能为空</font>
    </div>
    <input id="btn" type="button" value="submit"/>
    </form>
    <div id="myalert" class="hide">
    <div>
    <p>不能为空</p>
    <button>确定</button>
    </div>
    </div>
        <script>  
         var form = document.getElementsByTagName('form')[0];
         var inputboxs = form.getElementsByTagName('input');
         var state = false;
         var myalert = document.getElementById('myalert');
         myalert.height = document.body.clientHeight;
         alert(myalert.height);
         document.getElementById('btn').onclick = function(){
         for(var i in inputboxs){
         var input = inputboxs[i];
         if(input.type === 'text'){
         if(input.value === '' || typeof input.value === 'undefined'){
         state = true;
         var nextElem = input.nextSibling;
         while(nextElem){
         if(nextElem.tagName === 'FONT'){
         nextElem.className = '';
         break;
         }else{
         nextElem = nextElem.nextSibling;
         }
         }
         }
         }
         }
         if(state){
         myalert.className = '';
         }
        }
        myalert.getElementsByTagName('button')[0].onclick = function(){
         document.getElementById('tip').className = '';
         form.className = 'highlight';
         myalert.className = 'hide';
        }
        </script>  
    </body>  
    </html>  
      

  11.   

    上面有笔误,background-red: gray;改成background-color: gray;
      

  12.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
        <title> New Document </title>  
        <style>
         div{
         margin: 10px;
         }
         #tip{
         color: red;
         }
         .hide{
         display: none;
         }
         #myalert{
         position: absolute;
         top: 0;
         left: 0;
         z-index: 1000;
         width: 100%;
         height: 100%;
         background-color: gray;
         }
         #myalert div{
         width:200px;
         height: 100px;
         border: 1px solid blue;
         position: absolute;
         top:100px;
         left: 500px;
         }
         form.highlight{
         border: 1px solid red;
         }
        </style>
    </head>  
    <body>  
    <form>
    <div id="tip" class="hide">红色部分不能为空</div>
    <div>
        <input id="input_1" type="text" value=""/> 
    </div>
    <div>
        <input id="input_2" type="text" value=""/> 
    </div>
        <div>
        <input id="input_3" type="text" value=""/> 
    </div>
    <div>
        <input id="input_4" type="text" value=""/> 
    </div>
    <input id="btn" type="button" value="submit"/>
    </form>
    <div id="myalert" class="hide">
    <div>
    <p>不能为空</p>
    <button>确定</button>
    </div>
    </div>
        <script>  
         var form = document.getElementsByTagName('form')[0];
         var inputboxs = form.getElementsByTagName('input');
         var state = false;
         var myalert = document.getElementById('myalert');
         myalert.height = document.body.clientHeight;
         document.getElementById('btn').onclick = function(){
         for(var i in inputboxs){
         var input = inputboxs[i];
         if(input.type === 'text'){
         if(input.value === '' || typeof input.value === 'undefined'){
         state = true;
         input.style.backgroundColor = 'red';
         var nextElem = input.nextSibling;
         }
         }
         }
         if(state){
         myalert.className = '';
         }
        }
        myalert.getElementsByTagName('button')[0].onclick = function(){
         document.getElementById('tip').className = '';
         form.className = 'highlight';
         myalert.className = 'hide';
        }
        </script>  
    </body>  
    </html>  
      

  13.   

    20L  感谢您的如此多的代码!其实对于您的思路,我都理解,也想过,也能实现,要是项目没启动,我一定用您的方法!
    但是目前项目已经写到一大半了。很多都是统一用alert框弹出错误的js验证信息!(不止登录,注册)很多地方。。
    然后昨天老板看到项目效果就冒了一句,这系统的对话框太丑了!能不能改成其他地方的对话框(lhgdialog库的效果);
    当时我就着了。因为自定义的对话框没有阻塞的功能(能模态!但不能阻塞)!故而上来发贴求助!
    还是谢谢您啊!
      

  14.   

    这种方式,有想过,但是同样一个方法中有两个myalert弹出的代码,都是会执行的!!!
    谢谢您!!目前主要是不想改动原有代码!
    改动代价太大!!目前就想实现一个类似系统alert有阻塞功能的myAlert。。
      

  15.   

    感觉楼主你的思路太狭隘了,是不是为了用alert()来做注册验证?如果验证失败就alert()提示,并进行一个阻塞式的活动,说白了就是不执行下面的代码而已呗。想想其他的实现方式,为什么把自己的思路绑起来呢?
      

  16.   

    既然能自定义模态对话框,那么楼主应该是用的IE showModalDialog咯?如果这个不能阻塞,而你又非得要自定义alert,那么只能通过ActiveX来自定义对话框了,还不如你把所有alert的地方全部改一遍,改为函数回调。回头是岸。