JQuery 获取父元素方法

---恢复内容开始---

<tr class="removerow" style="">
                <td>
                    <input type="submit" name="ListView1$ctrl0$DeleteButton" value="删除" id="ListView1_DeleteButton_0" />
                    <input type="submit" name="ListView1$ctrl0$EditButton" value="编辑" id="ListView1_EditButton_0" />
                    <input type="button" isRemoveRow="true" value="无刷新删除" curId='1' />
                </td>
                <td>
                    <span id="ListView1_IdLabel_0">1</span>
                </td>
                <td>
                    <span id="ListView1_PostDateLabel_0">2008/8/8 0:00:00</span>
                </td>
                <td>
                    <span id="ListView1_MsgLabel_0">好啊</span>
                </td>
            </tr>
        
                
            <tr class="removerow" style="">
                <td>
                    <input type="submit" name="ListView1$ctrl1$DeleteButton" value="删除" id="ListView1_DeleteButton_1" />
                    <input type="submit" name="ListView1$ctrl1$EditButton" value="编辑" id="ListView1_EditButton_1" />
                    <input type="button" isRemoveRow="true" value="无刷新删除" curId='2' />
                </td>
                <td>
                    <span id="ListView1_IdLabel_1">2</span>
                </td>
                <td>
                    <span id="ListView1_PostDateLabel_1">2008/8/8 0:00:00</span>
                </td>
                <td>
                    <span id="ListView1_MsgLabel_1">沙发</span>
                </td>
            </tr>

查找父元素tr,将该元素删除。

$("input[curId=" + id + "]").parent().parent().remove();  //父元素的父元素 找到tr

$("input[curId=" + id + "]").parents("tr")[0].remove();  // 祖先元素 找到tr HTMLTableRowElement

$("input[curId=" + id + "]").parents(".removerow").remove(); //祖先元素 样式筛选

使用parents("tr") 通过tr元素查找,会返回2条数据,parents("tr").html() 能正常获取该行,如果html("") 无法正常修改。

删除该行tr,必须加parents("tr")[0].remove(); 才行。但是parents("tr")[0].html("")也无法正常修改, 不知道为什么会这样!

看来还是通过添加样式parents(".removerow") 操作方便些。

原文地址:https://www.cnblogs.com/han1982/p/3342060.html