简体   繁体   English

将元素从一个div移动到另一个

[英]Moving element from one div to another

Here is my live demo: https://jsfiddle.net/johndoe1992/3uqg7y9L/ 这是我的现场演示: https : //jsfiddle.net/johndoe1992/3uqg7y9L/

  1. When I click some button on the left panel it must be cloned to the right panel (see live demo) 当我单击左侧面板上的某个按钮时,必须将其克隆到右侧面板上(请参见现场演示)

  2. The bootstrap class 'disable' must be added on click to this button from the left panel too. 引导类“禁用”也必须在左侧面板上单击此按钮时添加。 We have the disabled button on the left and the enabled button on the right 我们在左侧有禁用按钮,在右侧有启用按钮

  3. When I click some button on the right panel it must relocate back to the left panel (see live demo) 当我单击右侧面板上的某个按钮时,它必须重新定位回到左侧面板(请参阅现场演示)

We have the enabled button on the left and no button on the right 我们在左侧有启用按钮,在右侧没有按钮

I've tried to find a solution using this, which works for #1 and #2, but I have no idea what to do with #3 我试图使用此方法找到一种解决方案,该解决方案适用于#1和#2,但是我不知道该如何处理#3

$(this).clone().appendTo('.selected');
$(this).prop('disabled', true);

Thank you for your help 谢谢您的帮助

Firstly you need to use a delegated event handler to handle clicks on the buttons you dynamically add to the .selected div. 首先,您需要使用委托事件处理程序来处理对动态添加到.selected div中的按钮的.selected

Secondly you need to add a way of identifying which button was clicked on in the .selected div and matching that with the original in the .base div. 其次,您需要添加一种方法来识别在.selected div中单击了哪个按钮,并将其与.base div中的原始按钮进行匹配。 To do that you could use a data attribute. 为此,您可以使用data属性。 From there you can just set the disabled property state and remove() the clone. 在这里,您可以仅设置disabled属性状态,然后remove()克隆。 Something like this: 像这样的东西:

<div class="base">
    <button type="button" class="btn btn-default" data-state="default">Text 1</button>
    <button type="button" class="btn btn-primary" data-state="primary">Text 2</button>
    <button type="button" class="btn btn-info" data-state="info">Text 3</button>
    <button type="button" class="btn btn-warning" data-state="warning">Text 4</button>
    <button type="button" class="btn btn-success" data-state="success">Text 5</button>
    <button type="button" class="btn btn-danger" data-state="danger">Text 6</button>
</div>
<div class="selected"></div>
$(document).on('click', '.btn', function() {
    if($(this).parent().attr("class") == "base") {
        $(this).clone().appendTo('.selected');
        $(this).prop('disabled', true);
    }
    else {
        $('.base').find('[data-state="' + $(this).data('state') + '"]').prop('disabled', false);
        $(this).remove();
    }
});

Updated fiddle 更新的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM