简体   繁体   English

挑 <li> 从div1,按钮单击,移动到div2

[英]Pick <li> from div1, on button click, move to div2

I know this is going to be an easy thing to do for someone with javascript experience, but I'm drawing a blank. 我知道对于有javascript经验的人来说,这将是一件容易的事,但我正在画一个空白。

I have a list of items: 我有一个项目列表:

<div id="left-side">
    <ul>
        <li><div>Item 1</div></li>
        <li><div>Item 2</div></li>
    </ul>
</div>

<input id="addElement" type="button" value="->"/>

<div id="right-side">
</div>

I would like to highlight(change the background color) the selected list item on the left and then on a click of the button, move the selected item to the right div, and finally changing the background color back. 我想突出显示(更改背景颜色)左侧所选列表项,然后单击按钮,将所选项目移动到右侧div,最后更改背景颜色。

I've seen this many, many times online. 我在网上看过很多很多次。 But can't for the life of me, come up with how to do it. 但不能为我的生活,想出怎么做。

I'd start by adding an empty <ul></ul> to your right side div, then use this: 我首先在你的右侧div添加一个空的<ul></ul> ,然后使用:

$('#left-side li').click(function () {
    $(this).toggleClass('selected');
});
$('#addElement').click(function () {
    $('#left-side li.selected').appendTo($('#right-side ul'));
});

jsFiddle example jsFiddle例子

Something like this (jquery) should do the trick: 像这样的东西(jquery)应该可以做到这一点:

// make the items selectable by toogling an 'active' class
$('#left-side li').click(function() {
     $(this).toggleClass('active');   
});

// on click of the move button
$('#addElement').click(function() {
    // get the items to move
    var $items =  $('#left-side li.active');
    // remove their active state
    $items.removeClass('active');
    // append them to the right side list
    $('#right-side ul').append($items);
});

As you can see the code is indeed pretty straigh forward. 正如您所看到的,代码确实非常直截了当。

I also set up a small example to demonstrate: http://jsfiddle.net/NbcS9/ 我还设置了一个小例子来演示: http//jsfiddle.net/NbcS9/

edit: 编辑:
If you only want to be able to only select a single item on the left, you could do something like this in stead: 如果您只想在左侧选择单个项目,则可以执行以下操作:

// make the items selectable by toogling an 'active' class
$('#left-side li').click(function () {
    // remove active class from all other items
    $('#left-side li').not($(this)).removeClass('active');
    // toggle the active class on the clicked item
    $(this).toggleClass('active');
});

And the updated fiddle: http://jsfiddle.net/NbcS9/1/ 更新的小提琴: http//jsfiddle.net/NbcS9/1/

You may try this 你可以试试这个

$(function(){
    $('#left-side ul li').on('click', function(){
        $(this).toggleClass('selected');
    });

    $('#addElement').on('click', function(){
        $('#left-side ul li.selected').appendTo($('#right-side ul'));
    });
});

DEMO. DEMO。

Using pure JavaScript would be tricky to do this, but using JQuery you can do this sort of easily. 使用纯JavaScript会很难做到这一点,但使用JQuery可以很容易地做到这一点。 Add click events to the two divs which would append the selected text of the other to itself. 将单击事件添加到两个div,它们将另一个的选定文本附加到自身。 to get the selected data add a function like this: 获取所选数据添加如下函数:

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

Also, I would look into Jquery Draggable(). 另外,我会研究Jquery Draggable()。 sounds like something that could relate to your desired end result. 听起来像是与你想要的最终结果有关的东西。 http://jqueryui.com/draggable/ http://jqueryui.com/draggable/

css: CSS:

  .highlighted { background: yellow; }

html: HTML:

<div id="left-side">
    <ul>
        <li><div>Item 1</div></li>
        <li><div>Item 2</div></li>
    </ul>
</div>

<input id="addElement" type="button" value="->"/>

<div id="right-side">
    <ul></ul>
</div>

JS: JS:

$('#left-side').find('li').on('click', function(event) {

     $(this)
        .siblings().removeClass('highlighted')
        .end()
        .addClass('highlighted');

});


$('#addElement').on('click', function(event) {
    event.preventDefault();
    $('#left-side').find('li.highlighted').appendTo('#right-side ul'));
});

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

相关问题 单击按钮将div1移出屏幕,将div2从顶部移至20px时会弹出动画效果 - bouncing animation effect when click on button to move div1 out of screen and div2 to 20px from top 如何移动创建的内容<div1>成一个<div2>不移动整体<div1>并不断创造元素<div1> ?</div1></div1></div2></div1> - How to move the created content of a <div1> into a <div2> without moving the whole <div1> and keep creating elements in <div1>? div2重叠时,是否可以同时单击div1和div2? - Is it possible to click div1 and div2 simultaneously, while div2 is overlaying? 如何在单击button1时隐藏div2并显示div1 angular - How to Hide div2 when button1 is clicked and show div1 angular 点击 <li> ,fadeOut(#div1),fadeIn相关索引div - Click on <li>, fadeOut(#div1), fadeIn related index div 将div1的内容放入div2 onclick事件 - put content of div1 into div2 onclick event 在div1上悬停时是否在div2上悬停时,其行为应有所不同 - when hover on div1 depenting on if it is on div2 or not it should act differently 计算角度div1和div2之间的距离 - Calculate distance between div1 and div2 in angular jQuery:在div1上的mousenter结束时显示div2 - jQuery: Show a div2 when mousenter over div1 is over 当div1和div2在同一行时连接线被隐藏 - connection line is hidden when div1 and div2 in same line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM