on click function jquery在用户选择后删除一个对象

时间:2017-12-19 22:11:41

标签: javascript jquery

在下面的函数chooseCharacter中,我如何调用三个对象也称为“thumbnail”,同时排除函数中指定为“this”的缩略图(然后,将其添加到我的HTML行“playerEnemies”中?

(进一步的细节:HTML索引文件有引导行,包括charactersAvailable,playerCharacter和playerEnemies。我可以清空charactersAvailable行,将点击的选项附加到playerCharacter,但我无法弄清楚如何选择来自charactersAvailable的未选中对象并推送它们或将它们附加到playerEnemies行)

$(document).ready(function() {
var char1 = {
    name: "Yoda",
    hp: 900,
    attackPower: 1,
    defendPower: 2,
}
console.log (char1);

var char2 = {
    name: "Jyn Erso",
    hp: 21,
    attackPower: 3,
    defendPower: 4,
}
console.log (char2);

var char3 = {
    name: "Lando Calrissian",
    hp: 31,
    attackPower: 5,
    defendPower: 6,
}
console.log(char3);

var char4 = {
    name: "Count Dooku",
    hp: 102,
    attackPower: 7,
    defendPower: 8,
}
console.log(char4);

var playerHP;

var playerAttackPower;

var defenderHP;

var charactersAvailable = [char1, char2, char3, char4];

var playerSelection;

function chooseCharacter () {

$(".thumbnail").on("click", function () {
        playerSelection = this;
        console.log(this);
        $(".charactersAvailable").empty();
        $("#playerCharacterTitle").text("Player Character");
        $(".playerCharacter").addClass("playerGameSelection");
        $(".playerCharacter").append(this);
        $("#playerEnemiesTitle").text("Enemies to Attack");

})

});

1 个答案:

答案 0 :(得分:0)

要在点击处理程序中选择所有其他.thumbnail对象:

$('.thumbnail').not(this);

...你应该能够用以下方式移动它们:

$('.thumbnail').not(this).appendTo('.enemies');