简体   繁体   English

将元素添加到 javascript 中的关联数组中

[英]Adding elements into associative array in javascript

I'm trying to add elements into an Associative Array, which is colors = [] I want it to have the ["id":selected_color] but my browser keeps crashing (an infinite loop somewhere?)我正在尝试将元素添加到关联数组中,即colors = []我希望它具有["id":selected_color]但我的浏览器不断崩溃(某个地方的无限循环?)

I'm not sure if I'm adding the elements into the array correctly.我不确定我是否正确地将元素添加到数组中。

What is happening is that I'm clicking on a span element, which has its ID value set to a hexcode value, and I'm trying to capture that value and associate it with the selected._color发生的事情是我单击了一个跨度元素,该元素的 ID 值设置为十六进制值,我试图捕获该值并将其与selected._color相关联

<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript">

var selected_color = "";
var colors = [];

$(document).ready(function() {
    $(".color_cell").click(function(){
        // ADD MY COLOR TO SELECTED COLOR'S ASSOCIATIVE ARRAY       
        colors[$(this).attr("id")] = selected_color;
        console.log($(this).attr("id"));
        $(this).css({'background-color':'white'});
        $(this).unbind('click');
        updateDisplay(colors);
        alert("hi");
    });

    $(".btnColor").click(function(){
         // MAKE SELECTED COLOR BE ME
        alert("hey");
        selected_color = $(this).attr("id");
    }); // end button handler
}); // end ready()

function updateDisplay(colors) {
    jQuery.each(colors, function(key, value) {
        //it seems to crash here...
        $("#storage_display").html("var "+$("#storage_display").html()+" " +value);
    });
};
</script>

You're defining colors as an array instead of an object.您将 colors 定义为数组,而不是 object。

You just need to initialize it properly:您只需要正确初始化它:

 var colors = {}; // or new Object();

Additional suggestion... there's really no need for the jQuery.each here.额外的建议......这里真的不需要 jQuery.each 。 Iterating over an associative array like this (let's not argue the semantics of whether you can actually call this an associative array) looks like this:像这样迭代一个关联数组(让我们不要争论是否可以真正将其称为关联数组的语义)看起来像这样:

function updateColors(colors)
{
    for (var key in colors)
    {
        $("#storage_display").html("var "+$("#storage_display").html()+" " +colors[key]);
    }
}

Why don't you try just some normal javascript instead of你为什么不尝试一些普通的 javascript 而不是

colors[$(this).attr("id")] = selected_color; 

try using尝试使用

colors.push(selected_color);

and instead of your jquery loop, try using而不是你的 jquery 循环,尝试使用

for(var i = 0; i < colors.length; i++) {
    $('#storage_display').html('whatever your trying to do here use colors[i]');
}

I dont understand what you're trying to do in the loop but assuming it did work, the html will be something like 'var var var var var var hexcode hexcode hexcode hexcode hexcode'我不明白你想在循环中做什么,但假设它确实有效,html 将类似于“var var var var var hexcode hexcode hexcode hexcode hexcode”

Apart from everything that was mentioned before ID attributes must begin with a letter AZ or az.除了前面提到的所有内容之外,ID 属性必须以字母 AZ 或 az 开头。 That might crash your browser.这可能会使您的浏览器崩溃。

Also if you are only storing colors in your array why not use just a normal array?此外,如果您只在阵列中存储 colors 为什么不使用普通阵列? (as James suggests) (正如詹姆斯建议的那样)

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

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