Javascript css不适用于列表中的所有ID
我正在尝试动态设置列出的一些div的css样式。当我迭代它们并设置它们的新css值时,只有最后一个div将获得新的css值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
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:你说得对:我会更新我的答案……谢谢。我发现了缺陷,谢谢。我发现了缺陷。