Jquery 最近的查找问题 - this.closest(..).查找不是一个函数



为什么我的(this).closest('.row').find('.col-lg-10').remove();不起作用并在日志中向我发送错误?

<div class="row section-first">
<div class="col-lg-2 text-center text-grayer my-auto p-2">
<p class="h6">Newbie</p>
<p class="h5">
<i class="fa fa-diamond m-1" aria-hidden="true"></i>
<i class="fa fa-diamond m-1" aria-hidden="true"></i>
</p>
<a href="http://localhost/forum/uzytkownik/krystian2160"><img class="mb-1 border-gray" src="http://localhost/forum/public/storage/avatars/1598320e3908c6.jpg" style="width: 112px; height: 112px;"/></a>
<p class="small m-0">Cirapuszka</p>
<!-----------------------------------------------------------------------------Reputacja----------->
<p class="m-0"><span class="badge bg-success"><i class="fa fa-plus-square" aria-hidden="true"></i> 0</span></p>
<p class="m-0"><span class="badge bg-inverse"><i class="fa fa-minus-square" aria-hidden="true"></i> 0</span></p>
<p class="text-muted">3 postów</p>
<!------------------------------------------------------------------------Button do edycji posta--->
<button class="btn btn-info btn-sm pointer post-edit">Edytuj Post</button>
</div>
<div class="col-lg-10 text-gray p-2">
<p>
Lorem ipsum dolor sit amet...
</p>
</div>
<div class="offset-lg-2 small col-lg-9">
</div>

这是我的网页 我这里有这个按钮

<button class="btn btn-info btn-sm pointer post-edit">
Edytuj Post
</button>

它在jquery中编写了这样的脚本:

$(document).on('click', 'button.post-edit', function(e){
e.preventDefault();
(this).closest('.row').find('.col-lg-10').remove();
});
Uncaught TypeError: this.closest(...).find is not a function
at HTMLButtonElement.<anonymous> (voluptas-eum-voluptatem-soluta-numquam-ipsum-totam-est:293)
at HTMLDocument.dispatch (jquery.js:3)
at HTMLDocument.q.handle (jquery.js:3)

当它像(this).closest('.row').remove();它删除整行时它有效,但这不是我打算做的。

必须删除包含此帖子中内容的col-lg-10

所以我尝试使用(this).closest('.row').find('.col-lg-10').remove();但正如我所说,这不起作用,我不知道为什么,因为我们首先通过在 DOM 中向上遍历closest然后向下遍历findcol-lg-10.row

这是怎么回事?

如问题注释中所述,您缺少 jQuery 标识符。

更改此设置:

(this).closest('.row').find('.col-lg-10').remove();

对此:

$(this).closest('.row').find('.col-lg-10').remove();

最新更新