<script type="text/javascript">
function cheng()
{
if(document.getElementById("usr").value=="")
document.getElementById("sec").style.color = "#ff0000";
}
</script><table id="td">
<tr>
<th id="sec">第一头</th>
<th>第二头</th>
</tr>
<tr>
<td>
<input type="text" id="usr">
</td>
<td>
<input type="button" value="check" onclick="cheng();">
</td></tr>
</table>

解决方案 »

  1.   

    <input type="text" id="usr">这个是不能有id的,这个是个面试题
      

  2.   


    <html>
    <head>
      <title>aaaa</title>
      
    </head>
    <body>
      <script type="text/javascript">
        function cheng(obj)
        {
          var input = getFirstChild(get_previoussibling(obj.parentNode));
          if(input.value=="")
            document.getElementById("sec").style.color = "#ff0000";
        }
        function get_previoussibling(n)  
        {  
          var x=n.previousSibling;  
          if(x == null) return null;  
          while (x && x.nodeType!=1)  
          {  
            x=x.previousSibling;  
          }  
          return x;  
        }       function getFirstChild(obj){
            for (i=0; i<obj.childNodes.length; i++){
              if (obj.childNodes[i].nodeType==1)
                return obj.childNodes[i];
              else 
                continue;
            } 
          }
        </script>    <table id="td">
          <tr>
            <th id="sec">第一头</th>
            <th>第二头</th>
          </tr>
          <tr>
            <td>
              <input type="text" name="usr">
            </td>
            <td>
              <input type="button" value="check" onclick="cheng(this);">
            </td>      </tr>
        </table>
      </body>
      </html>
      

  3.   

    <input type="text" id="usr">这个是不能有id的,这个是个面试题 if(document.getElementsByName("usr")[0].value=="")
    document.getElementById("sec").style.color = "#ff0000";
      

  4.   

    function cheng()
    {
        if(document.getElementsByName("usr")[0].value=="")
            document.getElementById("sec").style.color = "#ff0000";
    }
      

  5.   

    <script type="text/javascript" >
    function cheng() {
    if (document.getElementsByName("usr")[0].value == "") {
    document.getElementById("sec").style.color = "#ff0000";
    }
    }
    </script> 
      

  6.   

    按照你的想法,我这么做function cheng(obj){
    alert(obj.parentNode.previousSibling.nodeName);
    }打印出来的是#text,我视图打印文本节点的nodeValue来,但是怎么都是没有,这是为什么?
      

  7.   

    obj.parentNode.previousSibling 获取的是位于<td>之间的空文本节点,自然为空
      

  8.   

    如果cheng()函数没有this这个参数,该怎么做
      

  9.   

    <div id="n1" name="n2"></div>
    <div id="n2" name="n2"></div>
    <input type="text" name="n3"/>
    <script type="text/javascript">
    function testMain(){
    var names[]=document.getElementsByName("n3");
    alert("ss");
    /*
    for(var i=0;i<names.length;i++){
    alert(names[i]);
    }
    for(var x in names){
    alert(x);
    }*/
    }
    window.onload=testMain;
    </script>
    document.getElementsByName这个方法我用IE,chrome,firefox上试过,好像不能用,当我把var names[]=document.getElementsByName("n3");注释掉后,才能弹出ss,我在网站上看到有人说可以用,不知道我为什用不了