2011-11-10 188 views
-1

我有一個表單,我需要檢查沒有條目和類似的東西。現在,表單會將表單字段的顏色更改爲紅色,但我需要它將標籤更改爲紅色。由於標籤不是表單元素,因此我現在的方法使得更改標籤也變得非常困難。我可以實施的任何建議?使用javascript更改HTML標籤顏色

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Form Test</title> 
<script type="text/javascript"> 
    function validate(){ 
     var errors = new Array(); 
     for(var x = 0; x < document.forms[0].elements.length; x++){ 
      if(document.forms[0].elements[x].type == "text"){ 
       if(document.forms[0].elements[x].value == ""){ 
        errors.push("The " + document.forms[0].elements[x].name + " field cannot be blank.\n"); 
        document.forms[0].elements[x].className = "in_error"; 

       } 
      } 
      if(document.forms[0].elements[x].type == "select-one"){ 
       if(document.forms[0].elements[x].selectedIndex == 0){ 
        errors.push("The " + document.forms[0].elements[x].name + " field cannot be blank.\n"); 
        document.forms[0].elements[x].className = "in_error"; 
       } 
      } 
      if(document.forms[0].elements[x].type == "password"){ 
       if(document.forms[0].elements[x].value == ""){ 
        errors.push("The " + document.forms[0].elements[x].name + " field cannot be blank.\n"); 
        document.forms[0].elements[x].className = "in_error"; 
       } 
      } 
     } 

     if(errors.length == 0){ 
      return true;  
     }else{ 
      clear_errors(errors); 
      show_errors(errors); 
      return false; 
     } 
    } 
    function clear_errors(errors){ 
     var div = document.getElementById("errors"); 
     while(div.hasChildNodes()){ 
      div.removeChild(div.firstChild);  
     } 


    } 
    function show_errors(errors){ 
     var div = document.getElementById("errors"); 
     for(var x = 0; x < errors.length; x++){ 
      var error = document.createTextNode(errors[x]); 
      var p = document.createElement("p"); 
      p.appendChild(error); 
      div.appendChild(p) 
     } 
    } 

    window.onload = function(){ 
     document.forms[0].onsubmit = validate; 
    } 
</script> 

<style type="text/css"> 
    #errors{ 
     color: #F00; 
    } 
    .in_error{ 
     background-color: #F00; 
    } 
</style> 
</head> 

<body> 
<div id="errors"></div> 
<form action="http://ingenio.us.com/examples/echo" method="post"> 
    <div class="mainContainer" style="width:650px; margin: 0 auto; text-align:center;"> 
     <div class="contactInfo" style="text-align:center; width:650px;"> 
      <fieldset> 
       <legend style="text-align:left;">Contact Info</legend> 
       <label id="firstNameLabel" for="firstName">First Name: </label><input name="First Name" id="firstName" type="text" size="15" maxlength="15" />&nbsp;&nbsp; 
       <label id="lastNameLabel" for="lastName">Last Name: </label><input name="Last Name" id="lastName" type="text" size="15" maxlength="15" />&nbsp;&nbsp; 
       <label id="middleInitialLabel" for="middleInitial">Middle Initial: </label><input name="Middle Initial" id="middleInitial" type="text" size="4" maxlength="1" /><br /><br/> 
       <label id="streetAddressLabel" for="streetAddress">Street Address: </label><input name="Street Address" id="streetAddress" type="text" size="58" maxlength="55" /> <br /><br /> 
       <label id="cityLabel" for="city">City: </label><input name="City" id="city" type="text" size="30" maxlength="28" />&nbsp;&nbsp;  
       <label id="stateLabel" for="state">State: </label> 
       <select name="State" id="state"> 
        <option></option> 
        <option value="AL">Alabama</option> 
        <option value="AK">Alaska</option> 
        <option value="AZ">Arizona</option> 
        <option value="AR">Arkansas</option> 
        <option value="CA">California</option> 
        <option value="CO">Colorado</option> 
        <option value="CT">Connecticut</option> 
        <option value="DE">Delaware</option> 
        <option value="FL">Florida</option> 
        <option value="GA">Georgia</option> 
        <option value="HI">Hawaii</option> 
        <option value="ID">Idaho</option> 
        <option value="IL">Illinois</option> 
        <option value="IN">Indiana</option> 
        <option value="IA">Iowa</option> 
        <option value="KS">Kansas</option> 
        <option value="KY">Kentucky</option> 
        <option value="LA">Louisiana</option> 
        <option value="ME">Maine</option> 
        <option value="MD">Maryland</option> 
        <option value="MA">Massachusetts</option> 
        <option value="MI">Michigan</option> 
        <option value="MN">Minnesota</option> 
        <option value="MS">Mississippi</option> 
        <option value="MO">Missouri</option> 
        <option value="MT">Montana</option> 
        <option value="NE">Nebraska</option> 
        <option value="NV">Nevada</option> 
        <option value="NH">New Hampshire</option> 
        <option value="NJ">New Jersey</option> 
        <option value="NM">New Mexico</option> 
        <option value="NY">New York</option> 
        <option value="NC">North Carolina</option> 
        <option value="ND">North Dakota</option> 
        <option value="OH">Ohio</option> 
        <option value="OK">Oklahoma</option> 
        <option value="OR">Oregon</option> 
        <option value="PA">Pennsylvania</option> 
        <option value="RI">Rhode Island</option> 
        <option value="SC">South Carolina</option> 
        <option value="SD">South Dakota</option> 
        <option value="TN">Tennessee</option> 
        <option value="TX">Texas</option> 
        <option value="UT">Utah</option> 
        <option value="VT">Vermont</option> 
        <option value="VA">Virginia</option> 
        <option value="WA">Washington</option> 
        <option value="WV">West Virginia</option> 
        <option value="WI">Wisconsin</option> 
        <option value="WY">Wyoming</option> 
       </select>&nbsp;&nbsp; 
       <label name="zipLabel" for="zip">Zip: </label><input name="Zip" id="zip" type="text" size="7" maxlength="5" /><br /><br /> 
      </fieldset> 
     </div><br /><br /> 
    </div> 
    <div class="mainContainerTwo" style="width:450px; margin: 0 auto; text-align:center;"> 
     <div class="userInfo" style="text-align:center; width:450px;"> 
      <fieldset> 
       <legend style="text-align:left;">User Info</legend> 
       <label name="usernameLabel" for="username">Username: </label><input name="Username" id="username" type="text" size="20" maxlength="15" /><br /><br /> 
       <label name="passwordLabel" for="password">Password: </label><input name="Password" id="password" type="password" size="20" maxlength="15" /><br /><br /> 
       <label name="passwordConfirmLabel" for="passwordConfirm">Confirm Password: </label><input name="Confirm Password" id="passwordConfirm" type="password" size="20" maxlength="15" /><br /><br /> 
       <input type="submit" value="Submit" /> 
      </fieldset> 
     </div> 
    </div> 

    </form> 

</body> 
</html> 
+1

我建議簡化你的例子。 – BNL

回答

3

快速修復將從輸入元素遍歷到之前的兄弟元素,在本例中是標籤元素。之後,您可以將class in_error添加到您的標籤元素。

添加以下代碼行三,如果塊內

document.forms[0].elements[x].previousSibling.className = "in_error"; 

那麼你也應該改變你的CSS以下內容,以便設置紅色的背景顏色的輸入和紅色前景色到標籤。

input.in_error{ 
    background-color: #F00; 
} 

label.in_error { 
    color: #F00; 
} 

你有沒有任何理由不使用jQuery?如果沒有,我強烈建議你把它投入使用。它使得DOM操作變得非常簡單。

+0

+1關於使用jquery的建議 –

+1

我正在瀏覽javascript書籍。然後,我正在轉向jQuery – shinjuo