javascript - 如何在jquery的drop方法中调用函数?

标签 javascript jquery performance jquery-ui

我在 jquery 中有一些带有可拖放元素的代码。

这是我的 jsfiddle 链接 - http://jsfiddle.net/hirenwebdp/Mf6zJ/333/

我想通过为

创建一个函数来优化此代码
drop: function(event, ui) {
        var self = $(this);
        self.find(".placeholder").remove();
        var productid = ui.draggable.attr("data-id");
        if (self.find("[data-id=" + productid + "]").length) return;
        $("<li></li>", {
            "text": ui.draggable.text(),
            "data-id": productid
        }).appendTo(this);
             $("#equal").hide();

        // To remove item from other shopping chart do this
        var cartid = self.closest('.shoppingCart').attr('id');
        $(".shoppingCart:not(#"+cartid+") [data-id="+productid+"]").remove();
           var isAllFilled = true;
        $(".shoppingCart").each(function(){
            if($(this).find('ol .placeholder').length > 0)
            {
                isAllFilled = false;
                return
            }
                   });
        if(isAllFilled)
        {
            $("#equal").show();
             $("#equal").html("Congratulation! You have entered each block in correct place.");
        }

所以我已经编写了代码但无法正常工作。元素被拖动但不可放置。

优化后jsfiddle链接为http://jsfiddle.net/Mf6zJ/486/

那么如何解决这个问题。 请先检查两个 jsfiddle 链接,以便你们了解我面临的问题。

请帮助。

最佳答案

在你的第二个版本中传递eventui参数

function test(event, ui){ ...

因此你需要在函数中引用 elemnt 作为 this 调用 test 函数,如下所示

drop: function(event, ui) {

    test.call(this, event, ui);

}

DEMO

关于javascript - 如何在jquery的drop方法中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20393609/

相关文章:

javascript - jQuery - 滚动到 div 顶部后,我想动画/恢复固定的 div

php - Ajax php 在 mysql 中插入一些奇怪的 jquery 代码而不是问号

iphone - 在 iPhone 上使 UIImageView 不透明时出现问题

mysql - MySQL 上的 GROUP BY 非常慢。与指数无关

spring - Web 应用程序在嵌入式 tomcat 中的运行速度比在独立 tomcat 中快得多

javascript - 获取 HTML 的 ID

javascript - 我想在单击该 tr 中的按钮时找到最接近的 td

javascript - 在 Javascript 中使用 2D 数组内部的拼接

javascript - Modernizr.js 是否有助于在旧版浏览器上显示 css 3 和 html 5?如何?

php - AJAX 请求不发送阿拉伯字符