function login(){ 
var w = 300;
var h = 155;
var title = '登陆';
    var titleheight = "22pxpx"; // 提示窗口标题高度 
    var bordercolor = "#8B0000"; // 提示窗口的边框颜色 
    var titlecolor = "#FFFFFF"; // 提示窗口的标题颜色 
    var titlebgcolor = "#8B0000"; // 提示窗口的标题背景色 
    var bgcolor = "#FFFFFF"; // 提示内容的背景色 
    var msg;
    //var iWidth = document.documentElement.clientWidth; 
    //var iHeight = document.documentElement.clientHeight; 
    var iWidth = document.body.clientWidth;
    var iHeight = document.body.clientHeight;
    var bgObj = document.createElement("div");     //背景变暗宽高document.body.scrollLeft
    bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+window.screen.width+"px;height:"+Math.max(document.body.scrollHeight)+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;"; 
    document.body.appendChild(bgObj); 
    //DIV显示位置
    var msgObj=document.createElement("div"); 
    //msgObj.style.cssText = "position:absolute;font:11px '宋体';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:102;"; 
    msgObj.style.cssText = "position:absolute;font:11px '宋体';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px !important;height:170px;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:102;"; 
    document.body.appendChild(msgObj);     var table = document.createElement("table"); 
    msgObj.appendChild(table); 
    table.style.cssText = "margin:0px;border:0px;padding:0px;"; 
    table.cellSpacing = 0; 
    var tr = table.insertRow(-1); 
    var titleBar = tr.insertCell(-1); 
    titleBar.style.cssText = "width:100%;height:"+titleheight+"px;text-align:left;padding:3px;margin:0px;font:bold 13px '宋体';color:"+titlecolor+";border:1px solid " + bordercolor + ";cursor:move;background-color:" + titlebgcolor; 
    titleBar.style.paddingLeft = "10px"; 
    titleBar.innerHTML = title; 
    var moveX = 0; 
    var moveY = 0; 
    var moveTop = 0; 
    var moveLeft = 0; 
    var moveable = false; 
    var docMouseMoveEvent = document.onmousemove; 
    var docMouseUpEvent = document.onmouseup; 
    titleBar.onmousedown = function() { 
        var evt = getEvent(); 
        moveable = true; 
        moveX = evt.clientX; 
        moveY = evt.clientY; 
        moveTop = parseInt(msgObj.style.top); 
        moveLeft = parseInt(msgObj.style.left); 
         
        document.onmousemove = function() { 
            if (moveable) { 
                var evt = getEvent(); 
                var x = moveLeft + evt.clientX - moveX; 
                var y = moveTop + evt.clientY - moveY; 
                if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) { 
                    msgObj.style.left = x + "px"; 
                    msgObj.style.top = y + "px"; 
                } 
            }     
        }; 
        document.onmouseup = function () { 
            if (moveable) { 
                document.onmousemove = docMouseMoveEvent; 
                document.onmouseup = docMouseUpEvent; 
                moveable = false; 
                moveX = 0; 
                moveY = 0; 
                moveTop = 0; 
                moveLeft = 0; 
            } 
        }; 
    } 

    var closeBtn = tr.insertCell(-1); 
    closeBtn.style.cssText = "cursor:pointer; padding:2px;background-color:" + titlebgcolor; 
closeBtn.innerHTML = "<div style='font-size:12pt; color:"+titlecolor+";text-align:right;'>×</div>";
    closeBtn.onclick = function (){ 
        document.body.removeChild(bgObj); 
        document.body.removeChild(msgObj); 
    } 
    var msgBox = table.insertRow(-1).insertCell(-1); 
    msgBox.style.cssText = "font:10pt '宋体';"; 
    msgBox.colSpan = 2;
msg = "<div style='margin:5px 0 0 0;overflow:hidden;width:"+w+"px!important;width:275px'>"+
 "<div style='width:"+w+"px;overflow:hidden;'>"+
 "<div style='float:left;margin:10px 0 0 20px;line-height:20px;width:55px;'>用户名:</div>"+
 "<div style='float: left;margin:10px 0 0 0;'>"+
"<input type='text' name='login_name' id='login_name' style='border:1px solid #999;width:150px;'/>"+
"<span ><a href='#'>立即注册</a></span>"+
 "</div>"+
 "</div>"+
 "<div style='width:"+w+"px;overflow:hidden;'>"+
 "<div style='float:left;margin:10px 0 0 20px;line-height:20px;width:55px;'>密码:</div>"+
 "<div style='float: left;margin:10px 0 0 0;'>"+
"<input type='password' name='login_pass' onKeyDown='if(event.keyCode==32) return false'"+
  "id='password1' maxlength='16' onKeyPress='if (event.keyCode==32) {event.returnvalue=false;return false;}' style='border:1px solid #999;width:150px;'/>"+
"<span ><a href='#'>忘记密码</a></span>"+
 "</div>"+
 "</div>"+
 "<div style='width:"+w+"px;overflow:hidden;'>"+
 "<div style='float:left;margin:10px 0 0 20px;line-height:20px;width:55px;'>验证码:</div>"+
 "<div style='float: left;margin:10px 0 0 0;'>"+
"<input type='text' name='checkcode' style='border:1px solid #999;width:60px;'/>"+
 "</div>"+
 "<div style='float: left;margin:10px 0 0 0;'>"+
"<input type='text' name='code' style='border:1px solid #999;width:90px;'/>"+
"<span ><a href='#'>看不清楚</a></span>"+
 "</div>"+
 "</div>"+
 "<div style='width:"+w+"px;overflow:hidden;'>"+
   "<div style='float:left;margin:10px 0 0 170px !important;margin:10px 0 0 90px;'>"+
    "<input type='button' name='submit' value='确定' onClick='return createuser_login();' />"+
   "</div>"+
   "<div style='float:left;margin:10px 0 0 5px;'><input type='button' name='exit' value='取消'/></div>"+
 "</div>"+
    "</div>";
    msgBox.innerHTML = msg; 
        // 获得事件Event对象,用于兼容IE和FireFox 
    function getEvent() { 
        return window.event || arguments.callee.caller.arguments[0]; 

    } 

function createuser_login(){
var b=true; if(trim(document.getElementById('login_name').value)==""){
alert("对不起,请输入您的用户名!");
document.getElementById('login_name').select();
b=false;
return b;
}
if(document.getElementById('login_pass').value.indexOf(" ")>-1){
alert("登录密码不能包含空格!");
document.getElementById('login_pass').select();
b=false;
return b;
}
if(trim(document.getElementById('login_pass').value)==""){
alert("请输入您的登录密码!");
document.getElementById('login_pass').select();
b=false;
return b;
}
return b;
}点击确定按钮 时 出现错误 缺少对象!! 请高人指点 谢谢!!

解决方案 »

  1.   

    你应该把所有的代码贴出来,包括HTML,否则人没办法分析。
      

  2.   

    trim(document.getElementById('login_name').value)=="")
    js没有trim这个函数吧
      

  3.   

    找到原因了,是因为你缺少函数trim()。
    帮你写了个:
    function trim(str){
    return str.replace(/^\s+/gi, '').replace(/\s+$/gi, '');
    }<script type="text/javascript">
    function login(){ 
        var w = 300;
        var h = 155;
        var title = '登陆';
        var titleheight = "22pxpx"; // 提示窗口标题高度 
        var bordercolor = "#8B0000"; // 提示窗口的边框颜色 
        var titlecolor = "#FFFFFF"; // 提示窗口的标题颜色 
        var titlebgcolor = "#8B0000"; // 提示窗口的标题背景色 
        var bgcolor = "#FFFFFF"; // 提示内容的背景色 
        var msg;

        //var iWidth = document.documentElement.clientWidth; 
        //var iHeight = document.documentElement.clientHeight; 
        var iWidth = document.body.clientWidth;
        var iHeight = document.body.clientHeight;
        var bgObj = document.createElement("div");     //背景变暗宽高document.body.scrollLeft
        bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+window.screen.width+"px;height:"+Math.max(document.body.scrollHeight)+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;"; 
        document.body.appendChild(bgObj); 
        //DIV显示位置
        var msgObj=document.createElement("div"); 
        //msgObj.style.cssText = "position:absolute;font:11px '宋体';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:102;"; 
        msgObj.style.cssText = "position:absolute;font:11px '宋体';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px !important;height:170px;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:102;"; 
        document.body.appendChild(msgObj);     var table = document.createElement("table"); 
        msgObj.appendChild(table); 
        table.style.cssText = "margin:0px;border:0px;padding:0px;"; 
        table.cellSpacing = 0; 
        var tr = table.insertRow(-1); 
        var titleBar = tr.insertCell(-1); 
        titleBar.style.cssText = "width:100%;height:"+titleheight+"px;text-align:left;padding:3px;margin:0px;font:bold 13px '宋体';color:"+titlecolor+";border:1px solid " + bordercolor + ";cursor:move;background-color:" + titlebgcolor; 
        titleBar.style.paddingLeft = "10px"; 
        titleBar.innerHTML = title; 
        var moveX = 0; 
        var moveY = 0; 
        var moveTop = 0; 
        var moveLeft = 0; 
        var moveable = false; 
        var docMouseMoveEvent = document.onmousemove; 
        var docMouseUpEvent = document.onmouseup; 
        titleBar.onmousedown = function() { 
            var evt = getEvent(); 
            moveable = true; 
            moveX = evt.clientX; 
            moveY = evt.clientY; 
            moveTop = parseInt(msgObj.style.top); 
            moveLeft = parseInt(msgObj.style.left); 
             
            document.onmousemove = function() { 
                if (moveable) { 
                    var evt = getEvent(); 
                    var x = moveLeft + evt.clientX - moveX; 
                    var y = moveTop + evt.clientY - moveY; 
                    if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) { 
                        msgObj.style.left = x + "px"; 
                        msgObj.style.top = y + "px"; 
                    } 
                }     
            }; 
            document.onmouseup = function () { 
                if (moveable) { 
                    document.onmousemove = docMouseMoveEvent; 
                    document.onmouseup = docMouseUpEvent; 
                    moveable = false; 
                    moveX = 0; 
                    moveY = 0; 
                    moveTop = 0; 
                    moveLeft = 0; 
                } 
            }; 
        } 
        
        var closeBtn = tr.insertCell(-1); 
        closeBtn.style.cssText = "cursor:pointer; padding:2px;background-color:" + titlebgcolor; 
        closeBtn.innerHTML = "<div style='font-size:12pt; color:"+titlecolor+";text-align:right;'>×</div>";
        closeBtn.onclick = function (){ 
            document.body.removeChild(bgObj); 
            document.body.removeChild(msgObj); 
        } 
        var msgBox = table.insertRow(-1).insertCell(-1); 
        msgBox.style.cssText = "font:10pt '宋体';"; 
        msgBox.colSpan = 2;
        msg = "<div style='margin:5px 0 0 0;overflow:hidden;width:"+w+"px!important;width:275px'>"+
                         "<div style='width:"+w+"px;overflow:hidden;'>"+
                                 "<div style='float:left;margin:10px 0 0 20px;line-height:20px;width:55px;'>用户名:</div>"+
                                 "<div style='float: left;margin:10px 0 0 0;'>"+
                                        "<input type='text' name='login_name' id='login_name' style='border:1px solid #999;width:150px;'/>"+
                                        "<span ><a href='#'>立即注册</a></span>"+
                                 "</div>"+
                         "</div>"+
                         "<div style='width:"+w+"px;overflow:hidden;'>"+
                                 "<div style='float:left;margin:10px 0 0 20px;line-height:20px;width:55px;'>密码:</div>"+
                                 "<div style='float: left;margin:10px 0 0 0;'>"+
                                        "<input type='password' name='login_pass' onKeyDown='if(event.keyCode==32) return false'"+
                                                 "id='password1' maxlength='16' onKeyPress='if (event.keyCode==32) {event.returnvalue=false;return false;}' style='border:1px solid #999;width:150px;'/>"+
                                        "<span ><a href='#'>忘记密码</a></span>"+
                                 "</div>"+
                         "</div>"+
                         "<div style='width:"+w+"px;overflow:hidden;'>"+
                                 "<div style='float:left;margin:10px 0 0 20px;line-height:20px;width:55px;'>验证码:</div>"+
                                 "<div style='float: left;margin:10px 0 0 0;'>"+
                                        "<input type='text' name='checkcode' style='border:1px solid #999;width:60px;'/>"+
                                 "</div>"+
                                 "<div style='float: left;margin:10px 0 0 0;'>"+
                                        "<input type='text' name='code' style='border:1px solid #999;width:90px;'/>"+
                                        "<span ><a href='#'>看不清楚</a></span>"+
                                 "</div>"+
                         "</div>"+
                         "<div style='width:"+w+"px;overflow:hidden;'>"+
                                  "<div style='float:left;margin:10px 0 0 170px !important;margin:10px 0 0 90px;'>"+
                                          "<input type='button' name='submit' value='确定' onClick='return createuser_login();' />"+
                                  "</div>"+
                                  "<div style='float:left;margin:10px 0 0 5px;'><input type='button' name='exit' value='取消'/></div>"+
                         "</div>"+
                "</div>";
        msgBox.innerHTML = msg; 
            // 获得事件Event对象,用于兼容IE和FireFox 
        function getEvent() { 
            return window.event || arguments.callee.caller.arguments[0]; 
            
        } 
    } function trim(str){
    return str.replace(/^\s+/gi, '').replace(/\s+$/gi, '');
    }
    function createuser_login(){    
        var b=true;
        if(trim(document.getElementById('login_name').value)==""){
            alert("对不起,请输入您的用户名!");
            document.getElementById('login_name').select();
            b=false;
            return b;
        }
        if(document.getElementById('login_pass').value.indexOf(" ")>-1){
            alert("登录密码不能包含空格!");
            document.getElementById('login_pass').select();
            b=false;
            return b;
        }
        if(trim(document.getElementById('login_pass').value)==""){
            alert("请输入您的登录密码!");
            document.getElementById('login_pass').select();
            b=false;
            return b;
        }
        return b;
    }</script><body>
    <button onclick="login()">Show</button>
    </body>