如何使用 JavaScript 更改按钮的背景图像?
我想使用 Javascript 更改按钮的背景图像。这是我目前正在尝试的方法,但失败了。
HTML 代码 -
<tr id="Rank1">
<td><button class="darkSquare"></button></td>
<td><button class="lightSquare" ></button></td>
<td><button class="darkSquare" ></button></td>
<td><button class="lightSquare" ></button></td>
<td><button id="e1" class="darkSquare" ></button></td>
<td><button class="lightSquare" ></button></td>
<td><button class="darkSquare" ></button></td>
<td><button class="lightSquare"> </button></td>
</tr>
JavaScript 代码
initializer();
function initializer()
{
var tableRow = document.getElementById("Rank1");
var buttons = tableRow.getElementsByTagName("button");
for(b in buttons)
{
console.log(b.toString());
b.style.backgroundImage = "url('darkSquare.jpg')";
}
}
在控制台中,我收到一条错误消息,指出 b.style 未定义。
I want to change the background image of a button using Javascript. Here is what I am currently trying, but it is failing.
HTML code -
<tr id="Rank1">
<td><button class="darkSquare"></button></td>
<td><button class="lightSquare" ></button></td>
<td><button class="darkSquare" ></button></td>
<td><button class="lightSquare" ></button></td>
<td><button id="e1" class="darkSquare" ></button></td>
<td><button class="lightSquare" ></button></td>
<td><button class="darkSquare" ></button></td>
<td><button class="lightSquare"> </button></td>
</tr>
JavaScript Code
initializer();
function initializer()
{
var tableRow = document.getElementById("Rank1");
var buttons = tableRow.getElementsByTagName("button");
for(b in buttons)
{
console.log(b.toString());
b.style.backgroundImage = "url('darkSquare.jpg')";
}
}
In the console, I get an error saying b.style is undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
for (... in ...)
循环在 JavaScript 中不是这样工作的,它应该是:for (... in ...)
实际上迭代所有对象的“成员”,例如。在
for(var m in x) console.log(m)
中使用var x = {a: 1, b: 2, c: 3}
会产生这样的结果:与数组一起使用,因为它像这样考虑索引成员:
因为它为您提供索引,就好像它们是您无法区分的成员一样。陷阱是:
一般经验法则:仅在迭代对象中的成员时使用
for (... in ...)
,而使用for (var i = 0; i < array.length;
使用数组时你总是可以作弊:
for (... in ...)
loops do not work that way in JavaScript it should be:for (... in ...)
actually iterates over all the "members" of an objecteg. using
var x = {a: 1, b: 2, c: 3}
infor(var m in x) console.log(m)
will produceit kind of works with arrays because it considers the indices members like this:
since it is giving you the indices as if they were members you can't distinguish. the pitfall is:
General Rule of Thumb: only use
for (... in ...)
when iterating over members in an object, usefor (var i = 0; i < array.length; i++)
when using arraysyou can always cheat and use:
您应该将循环更改为
您使用的循环在变量 b 中分配键。由于您没有使用 hasOwnProperty 函数,因此此循环还可能产生您可能不想要的其他数据。
您还可以像这样使用 for-in 循环
You should change your loop as so
The loop you used assign keys in a variable b. Since you didn't use the hasOwnProperty function, this loop can also yield other data you might not want.
You could also use the for-in loop as so
使用
for ( ... in .... )
时,第一个参数是数组中的键,因此您必须像这样使用它:jsFiddle
when using
for ( ... in .... )
the first parameter is the key in the array so you have to use it like this:working example in jsFiddle