繁体   English   中英

从数组访问和显示对象

[英]access and display objects from an array

 $(document).ready(function () { var cardplace = 1; var card = [ { name: "Lord salazar" , atk: "12" , def: "3" , id: "1" } , { name: "Master manga" , atk: "8" , def: "10" , id: "2" } ]; $("#draw").click(function drawcard() { var monster = card[Math.floor(Math.random() * card.length)]; $('#' + cardplace).append('<div class="card"><div class="title"></div><div class="atk"></div><div class="def"></div>'); $(".title").text(monster.name); $(".atk").text(monster.atk); $(".def").text(monster.def); cardplace = cardplace + 1; console.log(cardplace); }); }); 
 .card-place{ width: 200px; height: 275px; background-color: saddlebrown; margin: 20px; float: left; } .card{ width: 180px; height: 255px; background-color: brown; overflow: auto; padding: 10px; } .title{ width: 100%; height: 30px; background-color: burlywood; margin-bottom: 10px; } .atk{ background-color: burlywood; width: 40px; height: 40px; float: left; } .def{ background-color: burlywood; width: 40px; height: 40px; float: right; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <body> <button id="draw">draw</button> <div class="card-place" id="1"> </div> <div class="card-place" id="2"></div> <div class="card-place" id="3"></div> </body> 

每次单击ID为#draw的按钮时,我想显示一个随机对象。 该对象出现,但是它总是替换变量Monster中的另一个对象。 所以我不能同时拥有不同的对象。 目前我有这个:

$(document).ready(function () {
var cardplace = 1;
var monster = [];
var card = [
    {
        name: "monster 1"
        , atk: "12"
        , def: "3"
    }
    , {
        name: "monster 2"
        , atk: "8"
        , def: "10"
    }
];
$("#draw").click(function drawcard() {
    monster[cardplace] = card[Math.floor(Math.random() * card.length)];
    $('#' + cardplace).append('<div class="card"><div class="title"></div><div class="atk"></div><div class="def"></div>');
    $(".title").text(monster[cardplace].name);
    $(".atk").text(monster[cardplace].atk);
    $(".def").text(monster[cardplace].def);
    cardplace = cardplace + 1;
    console.log(cardplace);
});
});

这是HTML

 <button id="draw">draw</button>
<div class="card-place" id="1">


</div>
<div class="card-place" id="2"></div>
<div class="card-place" id="3"></div>

您正在click处理程序中选择每个.title.atk.def元素。 可以创建一个变量来表示当前的#1#2#3元素,则设定contextjQuery()调用到当前上下文。

 $(document).ready(function() { var cardplace = 1; var card = [{ name: "Lord salazar", atk: "12", def: "3", id: "1" }, { name: "Master manga", atk: "8", def: "10", id: "2" }]; $("#draw").click(function drawcard() { var monster = card[Math.floor(Math.random() * card.length)]; var place = $('#' + cardplace); place.append('<div class="card"><div class="title"></div><div class="atk"></div><div class="def"></div>'); // set `context` to `place` at each of `jQuery()` call below $(".title", place).text(monster.name); $(".atk", place).text(monster.atk); $(".def", place).text(monster.def); if (cardplace === $("[class|=card][id]").length) { cardplace = 1 } else { cardplace += 1; }; }); }); 
 .card-place { width: 200px; height: 275px; background-color: saddlebrown; margin: 20px; float: left; } .card { width: 180px; height: 255px; background-color: brown; overflow: auto; padding: 10px; } .title { width: 100%; height: 30px; background-color: burlywood; margin-bottom: 10px; } .atk { background-color: burlywood; width: 40px; height: 40px; float: left; } .def { background-color: burlywood; width: 40px; height: 40px; float: right; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <body> <button id="draw">draw</button> <div class="card-place" id="1"></div> <div class="card-place" id="2"></div> <div class="card-place" id="3"></div> </body> 

暂无
暂无

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

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