gpt4 book ai didi

javascript - 将 id 从单击的 div 推送到我的数组中

转载 作者:行者123 更新时间:2023-11-28 04:58:01 24 4
gpt4 key购买 nike

我的问题是如何将点击的 div 中的 id 推送到我的数组中,然后比较该数组的长度是否与另一个数组一样长。任何帮助都会很棒!

$("#carrot").click (function (){
bunny.push("#carrot");
})

$("#lettice").click (function (){
bunny.push ("#lettice");
})

$("#flowers").click (function (){
bunny.push ("#flowers");
})


var rabbit=["dogs","chickens","chickens"];
var bunny =[];

function store (){
if (bunny.length>=rabbit.length){
alert ("yay!");
}}

最佳答案

您可以通过 this.id$(this).attr("id") 检索您当前的 ID。为了进行比较,您需要额外的按钮来检查是否相等。

You can try by clicking each button and see alert

var rabbit=["dogs","chickens","chickens"];
var bunny =[];

$("#carrot").click (function (){
bunny.push(this.id);
})

$("#lettice").click (function (){
bunny.push (this.id);
})

$("#flowers").click (function (){
bunny.push (this.id);
})

function store (){
if (bunny.length>=rabbit.length){
alert ("yay!");
}
else {
alert("No");
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="carrot">C</button>
<button id="lettice">L</button>
<button id="flowers">F</button>
<button onclick="store()">Equal</button>

关于javascript - 将 id 从单击的 div 推送到我的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42381801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com