JQuery 可拖动元素在放置时发生变化

发布于 2024-12-08 11:55:32 字数 1009 浏览 0 评论 0原文

我有一个看起来像这样的项目列表:

<div id="draggablearea">
    <ul>
        <li><div class="title">Title 1</div>
            <div class="content">Content 1.  Probably a few hundred characters</div></li>
        <li> ... </li>
    </ul> 
</div>

“内容”div 不可见,因为我的 css 包含此内容:

#content {display: none;}

现在,我使用 jquery-ui 使它们可拖动:

$(function(){
    $("#draggablearea li").draggable();
    .... (setting up droppable stuff)

到目前为止,一切都很好:所有内容都出现在我的可拖动列表,并且正在按预期移动到我的可放置区域。

现在,我想更改功能,以便在放置事件中,标题变得不可见,而内容变得可见。

我已经尝试过这个:

$("#droppableArea").droppable({
    accept: "#draggablearea li",
    drop: function(event, ui){
        ui.draggable.children(".content").appendTo(this);
    }
});

但它不起作用。这样做的正确方法是什么?

(当对整个 ui.draggable 对象调用 appendTo() 时,整个 li 会移动到可放置区域。)

I have a list of items that look like this:

<div id="draggablearea">
    <ul>
        <li><div class="title">Title 1</div>
            <div class="content">Content 1.  Probably a few hundred characters</div></li>
        <li> ... </li>
    </ul> 
</div>

The "content" div is not visible, because my css includes this:

#content {display: none;}

Now, I've made them draggable using jquery-ui:

$(function(){
    $("#draggablearea li").draggable();
    .... (setting up droppable stuff)

So far, so good: everything is appearing on my draggable list, and is moving onto my droppable area as expected.

Now, I want to change the functionality so that on the drop event, the title becomes invisible, and the content becomes visible.

I've tried this:

$("#droppableArea").droppable({
    accept: "#draggablearea li",
    drop: function(event, ui){
        ui.draggable.children(".content").appendTo(this);
    }
});

But it's not working. What's the right way to do this?

(The whole li is moved to the droppable area when the appendTo() is called on the whole ui.draggable object.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-12-15 11:55:32

也许还有类似这样的东西?

$("#droppableArea").droppable({
    accept: "#draggablearea li",
    drop: function(event, ui){
        ui.draggable.children(".title").hide();
        ui.draggable.children(".content").show();
    }
});

Perhaps something more like this?

$("#droppableArea").droppable({
    accept: "#draggablearea li",
    drop: function(event, ui){
        ui.draggable.children(".title").hide();
        ui.draggable.children(".content").show();
    }
});
酸甜透明夹心 2024-12-15 11:55:32

我认为您需要使内容可见。正如您所说,整个 div 已移动,因此这意味着内容 div 已经存在于 doppable 区域中。您只需将其显示样式设置为块或内联即可使其可见。

您将不得不尝试类似以下的操作。

ui.draggable.children(".content").css("display", "block")

I think you need to make the content visible. As you said the whole div was moved so it means contents div is already present in the doppablearea. You just need to it visible by setting its display style to block or inline.

You will have to try something like the following.

ui.draggable.children(".content").css("display", "block")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文