0

以下代码在 IE 中运行良好,但在 Chrome 或 Firefox 中运行良好。

如果有人可以提出更改建议,将不胜感激。

代码在以下链接中

http://www.4shared.com/office/HJNhTiwB/123.html

<html xmlns="http://www.w3.org/1999/xhtml&quot;&gt; 
<head runat="server"> 
<title>MRBN Textbox Sample</title> 
<style type="text/css"> 
.MRBN 
{ 
    width:145px; 
    font-family:Verdana; 
    font-size:12pt; 
    font-weight:bold; 
    text-transform: uppercase;
    color: #EDEBEC; 
} 
</style> 
</head> 
    <script type="text/javascript"> 
        function ValidateText(evnt) { 
            TxtLen = document.getElementById("TxtMRBN").value.length; 
            document.getElementById("TxtMRBN").style.color = "#33CC33"; 
            if (TxtLen < 5) { 
                if ((((evnt.keyCode > 64) && (evnt.keyCode < 91)) || ((evnt.keyCode > 96) && (evnt.keyCode < 123))) != true) { 
                    alert("1st letter/1st name +1st 4/last name..."); 
                    return false; 
                } 
            } 
            else if (TxtLen == 5) { 
                if (evnt.keyCode != 45) { 
                    alert("This needs dash or hyphen here..."); 
                    return false; 
                } 
            } 
            else if ((TxtLen > 5) && (TxtLen < 11)) { 
                    if (((evnt.keyCode > 47) && (evnt.keyCode < 58)) != true) { 
                        alert("Put last 5 of your phone number..."); 
                        return false; 
                    } 
            } 
            else { 
                alert("5-5. Now give it to Riders & WIN...!"); 
                return false; 
            } 
        }

    function ClearText() { 
        if (document.getElementById("TxtMRBN").value == "ALPHA-NUMBR") { 
            document.getElementById("TxtMRBN").value = ""; 
        } 
        return false; 
    } 
</script> 
<body> 
    <form id="form1" runat="server"> 
        <div> 
            <input type="text" id="TxtMRBN" class="MRBN" value="ALPHA-NUMBR" onkeypress="return ValidateText(event);" onmousedown="return ClearText();" /> 
        </div> 
    </form> 
</body> 
</html>

问候

4

3 回答 3

2

使用此代码在 IE、mozilla、chrome 中工作

    <html>
    <head> 
    <title>MRBN Textbox Sample</title> 
    <style type="text/css"> 
    .MRBN 
    { 
        width:145px; 
        font-family:Verdana; 
        font-size:12pt; 
        font-weight:bold; 
        text-transform: uppercase;
        color: #EDEBEC; 
    } 
    </style> 
    </head> 
        <script type="text/javascript"> 

            function ValidateText(evnt) { 
         var browserName=navigator.appName; 

       if (browserName=="Microsoft Internet Explorer")
       {
                TxtLen = document.getElementById("TxtMRBN").value.length; 
                document.getElementById("TxtMRBN").style.color = "#33CC33"; 
                if (TxtLen < 5) { 
                    if ((((evnt.keyCode > 64) && (evnt.keyCode < 91)) || ((evnt.keyCode > 96) && (evnt.keyCode < 123))) != true) { 
                        alert("1st letter/1st name +1st 4/last name..."); 
                        return false; 
                    } 
                } 
                else if (TxtLen == 5) { 
                    if (evnt.keyCode != 45) { 
                        alert("This needs dash or hyphen here..."); 
                        return false; 
                    } 
                } 
                else if ((TxtLen > 5) && (TxtLen < 11)) { 
                        if (((evnt.keyCode > 47) && (evnt.keyCode < 58)) != true) { 
                            alert("Put last 5 of your phone number..."); 
                            return false; 
                        } 
                } 
                else { 
                    alert("5-5. Now give it to Riders & WIN...!"); 
                    return false; 
                } 
           }
           else  //for firefox and chrome
           {
                 TxtLen = document.getElementById("TxtMRBN").value.length; 
                document.getElementById("TxtMRBN").style.color = "#33CC33"; 
                if (TxtLen < 5) { 
                    if ((((evnt.which > 64) && (evnt.which < 91)) || ((evnt.which > 96) && (evnt.which < 123))) != true) { 
                        alert("1st letter/1st name +1st 4/last name..."); 
                        return false; 
                    } 
                } 
                else if (TxtLen == 5) { 
                    if (evnt.which != 45) { 
                        alert("This needs dash or hyphen here..."); 
                        return false; 
                    } 
                } 
                else if ((TxtLen > 5) && (TxtLen < 11)) { 
                        if (((evnt.which > 47) && (evnt.which < 58)) != true) { 
                            alert("Put last 5 of your phone number..."); 
                            return false; 
                        } 
                } 
                else { 
                    alert("5-5. Now give it to Riders & WIN...!"); 
                    return false; 
                } 
           }
         }

        function ClearText() { 
            if (document.getElementById("TxtMRBN").value == "ALPHA-NUMBR") { 
                document.getElementById("TxtMRBN").value = ""; 
            } 

        } 
    </script> 
    <body> 

    <form id="form1" > 
            <div> 
                <input type="text" id="TxtMRBN" class="MRBN" value="ALPHA-NUMBR" onkeypress="return ValidateText(event);" onmousedown="ClearText();" /> 
            </div> 
      </form>
    </body> 
    </html>
于 2012-12-29T09:33:41.567 回答
0

将 keycode 更改为 which 并将 onmousedown 更改为 onclick

<html xmlns="http://www.w3.org/1999/xhtml&quot;&gt; 
<head runat="server">
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>MRBN Textbox Sample</title> 
<style type="text/css"> 
.MRBN 
{ 
    width:145px; 
    font-family:Verdana; 
    font-size:12pt; 
    font-weight:bold; 
    text-transform: uppercase;
    color: #EDEBEC; 
} 
</style> 
</head> 
    <script type="text/javascript"> 
        function ValidateText(evnt) {
            TxtLen = document.getElementById("TxtMRBN").value.length; 
            document.getElementById("TxtMRBN").style.color = "#33CC33"; 
            if (TxtLen < 5) { 
                if ((((evnt.which > 64) && (evnt.which < 91)) || ((evnt.which > 96) && (evnt.which < 123))) != true) { 
                    alert("1st letter/1st name +1st 4/last name..."); 
                    return false; 
                } 
            } 
            else if (TxtLen == 5) { 
                if (evnt.which != 45) { 
                    alert("This needs dash or hyphen here..."); 
                    return false; 
                } 
            } 
            else if ((TxtLen > 5) && (TxtLen < 11)) { 
                    if (((evnt.which > 47) && (evnt.which < 58)) != true) { 
                        alert("Put last 5 of your phone number..."); 
                        return false; 
                    } 
            } 
            else { 
                alert("5-5. Now give it to Riders & WIN...!"); 
                return false; 
            } 
        }

    function ClearText() { 
        if (document.getElementById("TxtMRBN").value == "ALPHA-NUMBR") { 
            document.getElementById("TxtMRBN").value = ""; 
        } 
        return false; 
    } 
</script> 
<body> 
    <form id="form1" runat="server"> 
        <div> 
            <input type="text" id="TxtMRBN" class="MRBN" value="ALPHA-NUMBR" onkeypress="return ValidateText(event);" onclick="return ClearText();" /> 
        </div> 
    </form> 
</body> 
</html>
于 2012-12-29T10:13:19.933 回答
0

您需要true在 ClearText 函数中返回:

function ClearText() { 
    if (document.getElementById("TxtMRBN").value == "ALPHA-NUMBR") { 
        document.getElementById("TxtMRBN").value = ""; 
    } 
    return true; 
} 

实际上 return false 允许元素的默认操作。因此,在您的情况下,该领域无法获得焦点。

我不确定为什么它在 IE 中有效。我想这是这个浏览器的一个错误。

于 2012-12-29T09:40:27.077 回答