使动态绘制的元素可拖动

时间:2013-05-08 10:35:07

标签: javascript jquery jquery-ui

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

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

代码写在

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

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

更新:

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

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);
        }
    });

}

1 个答案:

答案 0 :(得分: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"
            });
        }
    });
}