jquery:如何访问jquery对象中的数组元素

时间:2020-12-14 23:55:03

Heres the problem. I am building a jquery program that lets me create and position divs on a drawing area. I need to keep track of the divs, their location, CSS proterties and other details. So, I am creating an object that contains an array of all these items for each div.

继承人这个问题。我正在构建一个jquery程序,它允许我在绘图区域创建和定位div。我需要跟踪div,它们的位置,CSS保护和其他细节。所以,我正在创建一个对象,其中包含每个div的所有这些项的数组。

This is the code fragment for creating the object.

这是用于创建对象的代码片段。

var divData= {items: [
{ID: "21", Block: "Block_01", posX : "450", posY : "540" },
{ID: "43", Block: "Block_02", posX : "250", posY : "440" },
{ID: "46", Block: "Block_03", posX : "50", posY : "54" },
{ID: "54", Block: "Block_04", posX : "140", posY : "210" },
{ID: "55", Block: "Block_05", posX : "900", posY : "820" },
{ID: "79", Block: "Block_06", posX : "380", posY : "520" }
]};

And this is the code that I have written to display the contents of the array.

这是我编写的用于显示数组内容的代码。

alert ('array length is : ' + divData.items.length);

This statement returns a value of 6 which is correct, Also, I can push data into the array with this statement the alert statement now says 7.

这个语句返回的值是6,这是正确的。另外,我可以使用此语句将数据推送到数组中,alert语句现在显示为7。

divData.items.push({ID: "266", Block: "Block_01", posX : "450", posY : "540" });

The problem is when I try to display the contents of the array with the following code, nothing happens.

问题是当我尝试使用以下代码显示数组的内容时,没有任何反应。

for (var i=0;i<divData.items.length;i++)
{
document.write(divData.items[i] + "<br>");
}       

The output on the screen is this [object Object] seven times.

屏幕上的输出是这个[对象对象]七次。

What am I doing wrong?

我究竟做错了什么?

Any help will be much appreciated.

任何帮助都感激不尽。

Thanks

Chris

1 个解决方案

#1


2  

The 'items' in your array are objects. You can't output an object on the screen directly. You have to specify which property of the object you are trying to access:

数组中的“项”是对象。您无法直接在屏幕上输出对象。您必须指定要尝试访问的对象的哪个属性:

for (var i = 0;i < divData.items.length; i++)
{
  document.write(divData.items[i].ID + "<br/>"); // access the OBJECT's property!
}   

You may want to do something like:

您可能想要做以下事情:

for (var i = 0;i < divData.items.length; i++)
{
  $("#container").append("<div class='positionedDiv' id='" + divData.items[i].ID + "' style='top: " + divData.items[i].posY + "px;left:" + divData.items[i].posX + "px;></div>");
}   

Some CSS:

.positionedDiv {
  position: absolute; 
}

#1


2  

The 'items' in your array are objects. You can't output an object on the screen directly. You have to specify which property of the object you are trying to access:

数组中的“项”是对象。您无法直接在屏幕上输出对象。您必须指定要尝试访问的对象的哪个属性:

for (var i = 0;i < divData.items.length; i++)
{
  document.write(divData.items[i].ID + "<br/>"); // access the OBJECT's property!
}   

You may want to do something like:

您可能想要做以下事情:

for (var i = 0;i < divData.items.length; i++)
{
  $("#container").append("<div class='positionedDiv' id='" + divData.items[i].ID + "' style='top: " + divData.items[i].posY + "px;left:" + divData.items[i].posX + "px;></div>");
}   

Some CSS:

.positionedDiv {
  position: absolute; 
}