简体   繁体   English

jQuery 从元素中删除特定文本并将其添加到当前元素?

[英]jQuery remove specific text from elements and add it to an current element?

Lets say I have several Divs and a group of serial numbers.假设我有几个 Div 和一组序列号。 When I click on Div, there's a panel I can pass some numbers into the div;当我点击 div 时,有一个面板,我可以将一些数字传递给 div; And When I click on the 2nd div, I can pass the same numbers into current one but will remove duplicated numbers from the others.当我点击第二个 div 时,我可以将相同的数字传递给当前的一个,但会从其他 div 中删除重复的数字。

var arr = [1, 2, 3,...,10]

<div id="panel">
<button>1</button>
<button>2</button>
...
<button>10</button>
</div>

<div id="column">
<div class="card">1, 2</div>
<div class="card active">3, 4, 5, 6, 7</div>
<div class="card">8, 9, 10</div>
</div>

I came up with a problems: Passing the numbers to current div will remove all the others (including those never selected).我想出了一个问题:将数字传递给当前 div 将删除所有其他(包括从未选择的)。

$("#panel button").on("click", function(){
        $(this).toggleClass("selected");
        checkSelected()
})

function checkSelected(){
    var arr = [];
    $("#column .card.active").html("");
    for(var i = 0 ; i < $("#panel button .selected").length; i++){
        var num = $("#panel button .selected").eq(i).text()
        arr.push(num)
    }
    arr.sort(function(a, b){
        return a - b;
    });

    for(var j =0; j < arr.length; j++){
        var div = $("#column .card.active");
        var res = '<p>'+arr[j]+'</p>';
        div.append(res);
        // remove others...
        $("#column .card").not( ".active").find("p:contains("+arr[j]+")").filter(function () {
            return $(this).length > 0;
        }).remove();
    }
}

Updated: Just made a sample here jsfiddle更新:刚刚在这里做了一个示例jsfiddle

Anyone?任何人? Any help?有什么帮助吗? really appreciate it.真的很感激。

Maybe check wether the.remove() at the very end doesn't just discard all the numbers?也许在最后检查 the.remove() 是否不只是丢弃所有数字?

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

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