使用JQUERY在textarea中拖放按钮

时间:2016-12-19 12:55:37

标签: javascript jquery html css drag-and-drop

Hii Everyone,

        Below is my sample screenshot.

enter image description here

为了理解,我会再添加一张图片。 enter image description here

我想提供输入,如段落和空格数以及以下选项。最初我会选择段落所需的空白数量。基于选择按钮,它将像选择1,2等一样可见。 之后我会将该按钮拖放到段落textarea中,这样就可以将该位置作为第一个空白,同样适用于任意数量的选项。对于这种情况,我尝试了很多不同的东西,比如下面的link如果我拖放它会落在段落的初始位置或者段落的末尾。我想放在段落内的任何位置。我无法解决这个问题,所以我正在寻求帮助。

1 个答案:

答案 0 :(得分:1)

<强> HTML

<ul id="left-pane">
    <li>
        <div align="center" style="width:100px;border:3px solid #ddd; padding:5px;cursor:move;background:#eee;">Drag Me</div>
    </li>
</ul>

<ul id="right-pane"></ul>

<强> JS

$("#left-pane li").draggable({
    containment: '#gbox',
    cursor: 'move',
    helper: 'clone',
    scroll: false,
    connectToSortable: '#right-pane',
    appendTo: '#right-pane',
    start: function () {},
    stop: function (event, ui) {}
}).mousedown(function () {});

$("#right-pane").sortable({
    sort: function () {},
    placeholder: 'ui-state-highlight',
    receive: function () {},
    update: function (event, ui) {}
});

$("#right-pane li").live('dblclick', function () {
    $(this).remove();
})

$("#right-pane").droppable({
    accept: "#left-pane li",
    accept: ":not(.ui-sortable-helper)",
    drop: function (event, ui) {
        if ($(ui.draggable).find('.single-item').length == 0)
        {
            $(ui.draggable).html("<div align=\"center\" style=\"width:100px;border:3px solid #ddd; padding:5px;cursor:move;background:#eee;\">Drag Me</div>");
        }
    }
});

$("#left-pane").droppable({
    accept: "#right-pane li",
    drop: function (event, ui) {}
});

$("ul, li").disableSelection();

<强> CSS

#left-pane {
    border: 1px solid black;
    min-width: 100px;
    min-height: 100px;
}

#right-pane {
    border: 1px solid black;
    min-width: 100px;
    min-height: 100px;
}

.single-item {
    border:2px solid #ddd;
    background:#fff;
    margin:30px;
    padding:10px;
}

这与您的问题类似。请查看以下fiddle

另请查看此JSBin Demo