2013年6月2日 星期日

jQuery parent() && children() ( Select input from next td )

html:
<tr>
    <td>
        <input type="text" id="txtPrev" />
    </td>
    <td>
        <input type="text" id="txtNext" />
    </td>
</tr>
找到下一個 td 裡的 input:
var flag = 0;
$("input").keypress(function(){
    var target = $(this).parent("td").next().children("input");
    if(flag%2)
        target.css("background-color", "blue");
    else
        target.css("background-color", "green");
    ++flag;
});

Demo

沒有留言:

張貼留言