[jQuery]關於.siblings()

  • 2034
  • 0

摘要:[jQuery]關於.siblings()

可以取得同一層的元素,在官方的舉例說明中,是以HTML的 ul + li 做示範

我是用table的某一列 做測試

抓txt1的話,會顯示txt1隔壁的 TextBox1,tdb1,ddl1

HTML是這樣:


<table id="tableTest">
        <tr>
            <td>
                <asp:TextBox ID="txt1" runat="server" ></asp:TextBox>
                <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
                <asp:Button ID="tdb1" runat="server" Text="button1" />
                <asp:DropDownList ID="ddl1" runat="server"></asp:DropDownList>
            </td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
                <asp:Button ID="tdb2" runat="server"  Text="button2"  />
                <asp:DropDownList ID="ddl2" runat="server"></asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td colspan="2"><asp:Button ID="tdbTestSiblings" runat="server" Text="測試" OnClientClick="TestSiblings()" /></td>
        </tr>
    </table>

jQuery是這樣:


function TestSiblings() {
            $("input[id*=txt1]").siblings().each(function () {
                alert($(this).attr("id"));
            });
        }