直接上代码方便大家观看
<div id="divPanel" style="border-style:solid; border-color:Blue">
        <input type="text" id="Text1" value="txtOne" /><br />
        <input type="text" id="Text2" value="txtTwo" /><br />
        <input type="text" id="Text3" value="txtThree" /><br />
        <input type="text" id="Text4" value="txtFour" /><br />
        <input type="text" id="Text5" value="txtFive" /><br />
        <input type="text" id="Text6" value="txtSix" /><br />
    </div><br /><input type="button" value="获取divPanel内偶数的input"  onclick="getEvenInput();"/><br />function getEvenInput() {
            $("#divPanel>input:nth-child(even)").css("background", "#bbffaa");
        }奇怪的是无法获取,但如果去掉br就可以,像这样的情况大家如何处理呢?求解中
我的需求很简单 获取divPanel容器内所有的input,然后让这些为偶数索引的input背景色改变就行。

解决方案 »

  1.   

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>无标题文档</title>
    <script src="http://www.liuxiaofan.com/demo/js/jquery-1.6.4.min.js" type="text/javascript"></script>
    <style>
    *{margin:0; padding:0;}
    </style>
    </head>
    <body>
    <div id="divPanel" style="border-style:solid; border-color:Blue">
            <input type="text" id="Text1" value="txtOne" /><br />
            <input type="text" id="Text2" value="txtTwo" /><br />
            <input type="text" id="Text3" value="txtThree" /><br />
            <input type="text" id="Text4" value="txtFour" /><br />
            <input type="text" id="Text5" value="txtFive" /><br />
            <input type="text" id="Text6" value="txtSix" /><br />
        </div><br />
        <input type="button" value="获取divPanel内偶数的input"  onclick="getEvenInput();"/><br />
    <script>
    function getEvenInput() {
                $("#divPanel input:even").css("background", "#bbffaa");
            }
    </script>
    </body>
    </html>
      

  2.   

    把:even换成:odd  就是偶数
      

  3.   

    大神 我为什么用:nth-child(even) 就不行呢 求解!
      

  4.   

    先表示感谢,另外
    $("#divPanel>input:nth-child(even)").css("background", "#bbffaa");
    这个不是已经获取#divPanel中所有的input标记,然后再进行筛选了吗?为什么会失效呢!
      

  5.   


    <script>
    function getEvenInput() {
                $("#divPanel>input:odd").css("background", "#bbffaa");
            }
    </script>直接这样就行了~·不用加nth-child