2012-12-29 57 views
0

以下代碼在IE中運行良好,但在Chrome或Firefox中無效。在IE中工作不在Firefox或Chrome中

如果有人可以建議更改,將不勝感激。

的代碼是在下面的鏈接

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> 

問候

+0

發佈您的代碼。 – Blender

+0

沒有人在等待4shared和下載你的代碼..使用jsfiddle或粘貼 – geekman

+0

爲什麼你在''標籤之前有文字?什麼是「運作良好」;有什麼區別?你可以發佈產生的HTML(瀏覽器看到的)而不是原始的ASP.NET源代碼嗎? –

回答

2

使用此代碼的工作在IE,Mozilla中,鉻

<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> 
0

您需要返回ClearText功能中的true

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

實際上,返回false允許元素的默認操作。所以在你的情況下,該領域無法得到重點。

我不知道它爲什麼在IE中工作。我想這是這個瀏覽器的一個bug。

0

更改鍵碼作爲和這裏改變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> 
相關問題