使用 jQuery 删除具体元素
我认为这是一个非常简单的问题,但我无法处理它:我的 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()
(也许它不起作用,因为段落标记不是链接标记的父级)
有人可以帮我删除它?
I think that's a very simple question, but I can't deal with it: I have an unordered list in my jsp with some list items like these:
<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>
As you can see, every list item has a link to delete the message/answer. What I'm trying to do is, using jQuery, show an information message into the same li when I click the link, and delete all the content into the list item except that message.
Here's my jQuery code:
$('.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();
});
The above code deletes all except the paragraph tag. This is my resulting HTML code:
<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>
I want to delete this paragraph tag, but I tried these options with no luck:
$(this).closest('.li').children('p').remove();
and
$(this).closest('.text').remove()
(Maybe it doesn't work because the paragraph tag is not a parent of link tag)
Can someone help me to delete it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单:
搜索类
li
的元素,而不是元素。替换为:
将不起作用,因为
.closest()
执行以下操作:p.text
元素不是祖先,而是祖先的兄弟。因此,以下方法可能有效:Simple:
Searches for an element with class
li
, not a<li>
element. Replace with:Will not work, since
.closest()
does the following:The
p.text
element is not an ancestor, but a sibling to an ancestor. Thus, the following might have worked:try
而不是,
因为
.
li(前导点)意味着您正在搜索class="li"
,而不是本身
try
instead of
because
.
li (that leading dot) means you'Re searching forclass="li"
, not<li>
itselfli 不是类
$(this).closest('.li').children('p').remove();
使用
$(this).closest('li' ).children('p').remove();
li is not a class
$(this).closest('.li').children('p').remove();
use
$(this).closest('li').children('p').remove();