0

我认为这是一个非常简单的问题,但我无法处理:我的 jsp 中有一个无序列表,其中包含一些列表项,例如:

<li>
    <p class="text">Some text.</p>
    <table class="answer-details" style="width: 100%">
    <tbody>
        <tr class="tr">
            <td style="width: 50%;">
                <div class="msg-modification" display="inline" align="right">
                    <a id="modify" class="delete-answer" href="#">Delete</a>
                </div>
            </td>
            <td style="width: 50%; vertical-align: bottom;">
                <img src="...">
                <a class="answer-author" href="..." target="_blank">User name</a>
                <div class="answer-info">29/06/2011</div>
            </td>
        </tr>                               
    </tbody>
</table>

如您所见,每个列表项都有一个删除消息/答案的链接。我想要做的是,当我点击链接时,使用 jQuery 在同一个 li 中显示一条信息消息,并将除该消息之外的所有内容删除到列表项中。

这是我的 jQuery 代码:

$('.esborrar-resposta').click(function(event) {
    event.preventDefault();

    $(this).closest('.li').children('p').remove(); // it doesn't work
    $(this).closest('.tr').before('<tr><td><div class="output">Information message</div></td></tr>');
    $(this).closest('.tr').remove();
});

上面的代码删除了除段落标签之外的所有内容。这是我生成的 HTML 代码:

<li>
    <p>Some text.</p>
    <table class="answer-details" style="width: 100%">
            <tbody>
                    <tr><td><div class="output">Information message</div></td></tr>                             
            </tbody>
    </table>
</li>

我想删除这个段落标签,但我尝试了这些选项但没有成功:

$(this).closest('.li').children('p').remove();

$(this).closest('.text').remove()

(也许它不起作用,因为段落标签不是链接标签的父级)

有人可以帮我删除它吗?

4

3 回答 3

1

li 不是一个类 $(this).closest('.li').children('p').remove();

利用$(this).closest('li').children('p').remove();

于 2011-07-22T11:44:22.000 回答
1

尝试

$("li")

代替

$(".li")


$(this).closest('li').children('p').remove();

因为.li (那个前导点)意味着你正在搜索class="li",而不是<li>它本身

于 2011-07-22T11:42:19.250 回答
1

简单的:

$(this).closest('.li').children('p').remove();

搜索带有 class 的元素li,而不是<li>元素。用。。。来代替:

$(this).closest('li').children('p').remove();`

$(this).closest('.text').remove()

将不起作用,因为.closest()执行以下操作:

获取与选择器匹配的第一个祖先元素,从当前元素开始,向上遍历 DOM 树。(我的重点)

p.text元素不是祖先,而是祖先的兄弟。因此,以下可能有效:

$(this).closest('li').find('.text').remove();
于 2011-07-22T11:46:32.247 回答