jQuery 工具提示:remove() 遇到问题

发布于 2024-09-01 14:19:43 字数 1390 浏览 8 评论 0原文

我正在使用 jQuery 工具提示 插件。

我有这样的 HTML:

<li class="term ui-droppable">
    <strong>Fall 2011</strong>
    <li class="course ui-draggable">Biological Statistics I<a class="remove-course-button" href="">[X]</a></li>
    <div class="term-meta-data">
         <p class="total-credits too-few-credits">Total credits: 3</p>
         <p class="median-GPA low-GPA">Median Historical GPA:  2.00</p>
    </div>
</li>

我想删除 .course 元素。因此,我将一个点击处理程序附加到

function _addDeleteButton(course, term) {
    var delete_button = $('<a href="" class="remove-course-button" title="Remove this course">[X]</a>');
    course.append(delete_button);

    $(delete_button).click(function() {
        course.remove();
        return false;
    }).tooltip();
}

就附加点击处理程序而言,这一切都工作正常。但是,当调用 course.remove() 时,Firebug 会在 tooltip.js 中报告错误:

Line 282
tsettings is null

if ((!IE || !$.fn.bgiframe) && tsettings.fade) {

我做错了什么?如果链接附有工具提示,我是否需要专门将其删除?

更新:删除.tooltip()解决问题。我想保留它,但这让我怀疑我在这里使用 .tooltip() 是不正确的。

I'm using a jQuery tooltip plugin.

I have HTML like this:

<li class="term ui-droppable">
    <strong>Fall 2011</strong>
    <li class="course ui-draggable">Biological Statistics I<a class="remove-course-button" href="">[X]</a></li>
    <div class="term-meta-data">
         <p class="total-credits too-few-credits">Total credits: 3</p>
         <p class="median-GPA low-GPA">Median Historical GPA:  2.00</p>
    </div>
</li>

I want to remove the .course element. So, I attach a click handler to the <a>:

function _addDeleteButton(course, term) {
    var delete_button = $('<a href="" class="remove-course-button" title="Remove this course">[X]</a>');
    course.append(delete_button);

    $(delete_button).click(function() {
        course.remove();
        return false;
    }).tooltip();
}

This all works fine, in terms of attaching the click handler. However, when course.remove() is called, Firebug reports an error in tooltip.js:

Line 282
tsettings is null

if ((!IE || !$.fn.bgiframe) && tsettings.fade) {

What am I doing wrong? If the link has a tooltip attached, do I need to remove it specially?

UPDATE: Removing .tooltip() solve the problem. I'd like to keep it in, but that makes me suspect that my use of .tooltip() is incorrect here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事风中埋 2024-09-08 14:19:46

.tooltip() 插件有几个附加到元素的处理程序,即 mouseovermouseoutclick >。如果要删除该元素,只需 .unbind() all需要

delete_button.click(function() {
    course.unbind().remove();
    return false;
}).tooltip();

注意的另一个更改是 delete_button 已经是一个 jQuery 对象,再次将其包装在 $()只是克隆它 :)

发生错误是因为它试图在元素被删除后获取并使用它的设置数据虽然 .remove() 通过 jQuery.data(),其中 jQuery 从缓存中删除数据。这种方法只是阻止所有这些工具提示处理程序再次运行,因此数据消失(应该如此)并不重要,因为现在没有任何东西会寻找它。

The .tooltip() plugin has several handlers that attach to the element, namely on mouseover, mouseout, and click. If you're removing the element, just .unbind() all these events so they don't run, like this:

delete_button.click(function() {
    course.unbind().remove();
    return false;
}).tooltip();

One other change to note is that delete_button is already a jQuery object, wrapping it in $() again just clones it :)

The error happens because it's trying to fetch and use it's settings data off the element after it's been though .remove() via jQuery.data(), in which jQuery removes it's data from the cache. This approach this just prevents all those tooltip handlers from running again, so it won't matter that the data's gone (as it should be), because nothing will be looking for it now.

唠甜嗑 2024-09-08 14:19:45

我遇到了同样的问题,最终不得不延迟 .remove() 的调用以防止出现错误。因此,类似以下内容可能适合您:

$(delete_button).click(function() {
   setTimeout( function() { course.remove() } , 1);
   return false;
}).tooltip();

I had this same issue and ended up having to delay the call of .remove() to prevent the error. So something like the following may work for you:

$(delete_button).click(function() {
   setTimeout( function() { course.remove() } , 1);
   return false;
}).tooltip();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文