如何在'stop'事件期间定位克隆的可拖动元素?



>我有一个可拖动的列表元素,它可以在拖动时克隆并填充另一个列表。当我放下项目时,我想更改新放下的 li 元素的内容。我遇到的问题是jQuery的仍然是原始的拖动项,而不是克隆的项。

如何专门定位新克隆的项目,以便我可以更改其内容而不影响列表中的其他项目?

代码如下。感谢您的任何智慧!

.CSS:

<style>
#intro-clone, #assessment-clone {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 180px;
    display: inline-block;
}
#intro-statements, #assessment-statements {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 365px;
}
#intro-clone li, #assessment-clone li, #intro-statements li, #assessment-statements li {
    margin: 0 5px 5px 5px;
    padding: 5px;
    font-size: 1.2em;
    min-height: 25px;
}
#intro-clone li, #assessment-clone li, #intro-statements li, #assessment-statements li {
    min-height: 25px;
    line-height: 1.2em;
}
.ui-state-highlight {
    min-height: 25px;
    line-height: 1.2em;
}
ul {
    padding: 6px 1px 1px 0 !important;
    min-height: 42px;
    background: silver;
    border-radius: 3px;
}
</style>

.JS:

<script>
$(function() {
    $("#intro-statements, #assessment-statements").sortable({
        placeholder: "ui-state-highlight",
        revert: true
    });
    $( "#intro-clone li" ).draggable({
        drag: function() { $("#intro-clone li").css("width","158px"); },
        stop: function() {
            $("#intro-statements li").css({"width":"343px","height":"25px"}); // this is applied to ALL the li elements, including the cloned one
            console.log(this); // reference's the original drag element, not the clone
            window.setTimeout(function() { $(this).html("new content") }, 1000); // doesn't work!
        },
        connectToSortable: "#intro-statements",
        helper: "clone",
        revert: "invalid"
    });
    $( "#assessment-clone li" ).draggable({
        drag: function() { $("#assessment-clone li").css("width","158px"); },
        stop: function() {
            $("#assessment-statements li").css({"width":"343px","height":"50px"}).html("new content<br>new line");  // this is applied to ALL the li elements, including the cloned one
        },
        connectToSortable: "#assessment-statements",
        helper: "clone",
        revert: "invalid",
    });
    $("#intro-statements, #assessment-statements").disableSelection();
    $("#show").click(function() {
        var intros = {};
        $('li', 'ul.sort').each(function(i) {
            var $li = $(this);
            var $text = $li.text();
            var name = $li[0].tagName.toLowerCase();
            //intros[name + '-' + i] = $text;
            intros[i] = $text;
        });
        console.log(intros);
        $("#show").html(JSON.stringify(intros));
    });
});
</script>

.HTML:

<ul id="intro-clone">
    <li class="ui-state-default">Intro item</li>
</ul>
<ul id="assessment-clone">
    <li class="ui-state-default">Assessment item</li>
</ul>
<br>
<br>
<ul id="intro-statements" class="sort">
</ul>
<br>
<ul id="assessment-statements" class="sort">
</ul>
<div id="show">SHOW</div>
疯狂的是,

你如何永远试图找到解决方案,然后当你发布寻求帮助时,它就会来找你!

所以这为我做了诀窍:我在拖动事件上将一个类附加到元素。当删除新元素时,我从源元素中删除该类,并将该类定位到新的克隆元素上,并对其进行更改。最后,我从克隆的元素中删除该类以保持其唯一性。

以下是更新的 js:

$( "#intro-clone li" ).draggable({
    drag: function() {
        $("#intro-clone li").css("width","158px").addClass("new"); // add the new class
    },
    stop: function() {
        $("#intro-clone li").removeClass("new"); // remove it again
        $("#intro-statements li.new").css({"width":"343px","height":"25px"});
        window.setTimeout(function() { $("#intro-statements li.new").html("new content") }, 1000); // make my changes to the cloned element
        window.setTimeout(function() { $("#intro-statements li").removeClass("new") }, 1500); // remove class so it is unique for the next clone
    },
    connectToSortable: "#intro-statements",
    helper: "clone",
    revert: "invalid"
});

这可能是很好的代码,也可能不是,但它解决了我的问题,所以我很高兴:)

您需要将stop添加到sortable方法中,如下所示sortable使draggabledroppable在一起。我认为这对你更好。

$("#intro-statements, #assessment-statements").sortable({
    placeholder: "ui-state-highlight",
    revert: true,
    stop: function(event, ui) {
        console.log(event.target);
    }
});

最新更新