selected added div be draggable

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 01:26:57

问题


I want my added div be draggable.
I can do it by adding $("div[id^=new-text]:last").draggable();
But this is not work.

My complete example link

This is the code:

var count=0;
$(function() {
    $(".add-new").click(function() {
        $('.image').append("<div id='new-text_"+count+"' contenteditable>Edit me</div>");
        count++;
    });

    $('#writable').keydown(function() {
        var writable_value = $('#writable').val();
        $("div[id^=new-text]:last").text(writable_value);
    });

    /*change font family*/
    $("#fs").change(function() {
        //alert($(this).val());
        $("div[id^=new-text]:last").css("font-family", $(this).val());
    });

    $("#fc").change(function() {
        //alert($(this).val());
        $("div[id^=new-text]:last").css("color", $(this).val());
    });

    /* change font size */
    $("#size").change(function() {
        $("div[id^=new-text]:last").css("font-size", $(this).val() + "px");
    });

    $("div[id^=new-text]:last").draggable();
})

回答1:


If you want any of the added text to be draggable. You will be looking for the following:

$("div[id^=new-text]").draggable();

Instead of this what you have currently:

$("div[id^=new-text]:last").draggable();

You was only picking the last new-text element, so you need to select all the elements with the id containing new-text.




回答2:


If I add draggable it is work but is problem with editing of selected div. jsfiddle

        $(".add-new").click(function() {
        $('.image').append("<div id='new-text_"+count+"' contenteditable>Edit me</div>");
        count++;
    });


来源:https://stackoverflow.com/questions/33147401/selected-added-div-be-draggable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!