Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript css不适用于列表中的所有ID_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript css不适用于列表中的所有ID

Javascript css不适用于列表中的所有ID,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在尝试动态设置列出的一些div的css样式。当我迭代它们并设置它们的新css值时,只有最后一个div将获得新的css值 for (var i = 0, l = list.length; i < l; i++) { b = list[i]; b.$block.css({ "top": b.pos.y + "px", "left": b.pos.x + "px" }); b.pos = b.pos.add(b.vec); } for(变量i=0,l=l

我正在尝试动态设置列出的一些div的css样式。当我迭代它们并设置它们的新css值时,只有最后一个div将获得新的css值

for (var i = 0, l = list.length; i < l; i++)
{
  b = list[i];

  b.$block.css({
    "top": b.pos.y + "px",
    "left": b.pos.x + "px"
  });

  b.pos = b.pos.add(b.vec);
}
for(变量i=0,l=list.length;i
b、 $block是jQuery对象。b、 pos和b.vec只是一些Vector2D实例,我使用它们来计算所有div的顶部和左侧值。

您能尝试使用jQuery函数吗?这可能会解决你的问题

可能就是这样

$.each(b.$block, function( index, value ) {
  $(value).css({"top" , b.pos.x + "px"});
});
您可以尝试使用jQuery函数吗?这可能会解决你的问题

可能就是这样

$.each(b.$block, function( index, value ) {
  $(value).css({"top" , b.pos.x + "px"});
});
css()方法确实会影响集合中的所有jQuery对象,但如果在调试器中运行代码,则可以看到每次都传递相同的对象:div#block3

您的问题位于blockSim(list)方法之外的其他地方,因为您正在传递同一对象的列表

css()方法确实会影响集合中的所有jQuery对象,但是如果在调试器中运行代码,您可以看到每次都在传递相同的对象:div#block3

您的问题位于blockSim(list)方法之外的其他地方,因为您正在传递同一对象的列表

新解决方案 您忘记了三次关键字
new

return (Vector2D(that.x + other.x, that.y + other.y)); // line 92

return (Vector2D(that.x - other.x, that.y - other.y)); // line 96

metaList.push(createBlock(i)); // line 133
你必须写作

return (new Vector2D(that.x + other.x, that.y + other.y)); // line 92

return (new Vector2D(that.x - other.x, that.y - other.y)); // line 96

metaList.push(new createBlock(i)); // line 133
仅使用
new
关键字,函数的
参数设置为新值。代码如下:

老办法 这里有一个已调试的代码示例:

考虑函数
createBlock()

对于函数
Vector2D()

新解决方案,您必须进行相同的更正 您忘记了三次关键字
new

return (Vector2D(that.x + other.x, that.y + other.y)); // line 92

return (Vector2D(that.x - other.x, that.y - other.y)); // line 96

metaList.push(createBlock(i)); // line 133
你必须写作

return (new Vector2D(that.x + other.x, that.y + other.y)); // line 92

return (new Vector2D(that.x - other.x, that.y - other.y)); // line 96

metaList.push(new createBlock(i)); // line 133
仅使用
new
关键字,函数的
参数设置为新值。代码如下:

老办法 这里有一个已调试的代码示例:

考虑函数
createBlock()


您必须对函数
Vector2D()

进行相同的修正,它应该可以正常工作。您可以使用
$。每个
在列表中进行迭代。还要考虑调试你的代码,看看<代码> B.Bug 是否是一个jQuery对象。如果它只是一个元素,您可以将它包装在
$(element)
中,从中生成jQuery集合。它应该可以正常工作。您可以使用
$。每个
在列表中进行迭代。还要考虑调试你的代码,看看<代码> B.Bug 是否是一个jQuery对象。如果它只是一个元素,您可以将它包装在
$(element)
中,从中生成jQuery集合。谢谢,我还必须将您建议的解决方案应用于Vector2D函数。我觉得我太倾向于Python了。@maikovich:你说得对:我会更新我的答案……谢谢,我也不得不将你建议的解决方案应用到Vector2D函数中。我觉得我太倾向于Python了。@maikovich:你说得对:我会更新我的答案……谢谢。我发现了缺陷,谢谢。我发现了缺陷。