jquery怎么獲取tr下的第二個td


1、使用css選擇器,$("tr td:nth-child(2)")。

2、使用遍歷函數eq()。

下面就以上兩個方法進行實例演示:單擊按鈕改變所有行的第二個單元格的樣式,單擊任意行改變該行第二個單元格的樣式。

1、HTML結構

<table id = "test">
    <tr><td>1</td><td>1</td><td>2</td><td>3</td></tr>
    <tr><td>2</td><td>4</td><td>5</td><td>6</td></tr>
    <tr><td>3</td><td>7</td><td>8</td><td>9</td></tr>
    <tr><td>4</td><td>1</td><td>2</td><td>3</td></tr>
</table>
<input type="button" id="btn" value="設置">

2、jquery代碼

$(function(){
    $("#btn").click(function() {
        $("#test tr td:nth-child(2)").addClass('red');
    });
    $("#test tr").click(function() {
        $(this).children('td').eq(1).addClass('red');
    });
});

 

3、效果演示


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM