Jquery最近找到问题 - this.closest(...)。find不是函数

时间:2017-08-21 17:45:06

标签: javascript jquery html dom

为什么我的(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>

这是我的HTML 我有这个按钮

<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();,但正如我所说,这不起作用而且我不知道为什么,因为我们首先通过遍历{中的UP来.rowclosest {1}}然后按find向下col-lg-10

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

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

更改此:

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

对此:

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