将id从单击的div推入我的数组

时间:2017-02-22 02:50:42

标签: javascript jquery html arrays

我的问题是如何将id从被点击的div推入我的数组,然后比较数组的长度是否与其他数组一样长。任何帮助都会很棒!

$("#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!");
}}

1 个答案:

答案 0 :(得分:0)

您可以按In [1]: from collections import namedtuple In [2]: entry = namedtuple("Entry", ("id", "value")) In [3]: A = [{'id':'u2s8Et', 'value':'David'}, {'id':'u2s0PW', 'value':'Linda'}] ...: B = [{'id':'u2s8Et', 'value':'David'}, {'id':'u2s7PA', 'value':'Steven'}] ...: In [4]: def parse_to_entry(values): ...: return {entry(d["id"], d["value"]) for d in values} ...: In [5]: parse_to_entry(A) Out[5]: {Entry(id='u2s0PW', value='Linda'), Entry(id='u2s8Et', value='David')} In [6]: parse_to_entry(A).difference(parse_to_entry(B)) Out[6]: {Entry(id='u2s0PW', value='Linda')} In [7]: parse_to_entry(A).intersection(parse_to_entry(B)) Out[7]: {Entry(id='u2s8Et', value='David')} this.id检索当前ID。要比较你需要额外的按钮来检查是否相等。

  

您可以尝试点击每个按钮并查看提醒

$(this).attr("id")
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");
   }
}