0

我有以下代码可以使元素可拖动!

$('.item').draggable({
        revert : true,
        zIndex: 1,
        containment: "window"
    });

并且代码是写在里面的

jQuery(window).ready(function(){})

这些项目元素是动态淹没的,所以当页面加载一次时,项目是不可拖动的!但是当我刷新页面时,它变成了可拖动的。第一次加载页面时,如何使元素可拖动。!

更新 :

这是我用来动态绘制元素的函数。

function populateUIItems(majorGroupId){
    var uiElement = $('.item').first();
    $('#item-list-section').empty();

    ajaxCall("/getItemByMajorGroup?mGroup=Beverage","GET",null,function(response){
        for(var i = 0; i < response.itemModel.length; i++){
            var clone = uiElement.clone();
            clone.children('.item-price').empty();
            clone.children('.item-price').append("<h4>" + response.itemModel[i].sellingPrice + "</h4>");
            clone.children('.item-name').empty();
            clone.children('.item-name').append("<h4>" + response.itemModel[i].description + "</h4>");
            clone.attr('id',response.itemModel[i].itemId);
            clone.css('background-image', "url(../images/item_bg.jpg)");
            $('#item-list-section').append(clone);
        }
    });

}
4

1 回答 1

1

正如我在评论中提到的,您需要为动态添加的元素调用小部件。

function populateUIItems(majorGroupId){
    var uiElement = $('.item').first();
    $('#item-list-section').empty();

    ajaxCall("/getItemByMajorGroup?mGroup=Beverage","GET",null,function(response){
        for(var i = 0; i < response.itemModel.length; i++){
            var clone = uiElement.clone();
            clone.children('.item-price').empty();
            clone.children('.item-price').append("<h4>" + response.itemModel[i].sellingPrice + "</h4>");
            clone.children('.item-name').empty();
            clone.children('.item-name').append("<h4>" + response.itemModel[i].description + "</h4>");
            clone.attr('id',response.itemModel[i].itemId);
            clone.css('background-image', "url(../images/item_bg.jpg)");
            clone.appendTo('#item-list-section').draggable({
                revert : true,
                zIndex: 1,
                containment: "window"
            });
        }
    });
}
于 2013-05-08T10:44:02.887 回答