如何使用 JavaScript 更改按钮的背景图像?

发布于 2025-01-08 06:28:10 字数 1310 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

聊慰 2025-01-15 06:28:10

for (... in ...) 循环在 JavaScript 中不是这样工作的,它应该是:

for (var b = 0; b < buttons.length; b++) {
    buttons[b].style.backgroundImage = "url('darkSquare.jpg')";
}

for (... in ...) 实际上迭代所有对象的“成员”,

例如。在 for(var m in x) console.log(m) 中使用 var x = {a: 1, b: 2, c: 3} 会产生

> a
> b
> c

这样的结果:与数组一起使用,因为它像这样考虑索引成员:

var x = [1,2,3];
for (var m in x) console.log(m);
> 0
> 1
> 2

因为它为您提供索引,就好像它们是您无法区分的成员一样。陷阱是:

var x = [1,2,3];
x.stuff = 'boo!';
for (var m in x) console.log(m);
> 0
> 1
> 2
> stuff

一般经验法则:仅在迭代对象中的成员时使用 for (... in ...),而使用 for (var i = 0; i < array.length; 使用数组时

你总是可以作弊

for (var i = 0, item = x[i]; i < x.length; item=x[++i]) {
    // now item is the current item
}

for (... in ...) loops do not work that way in JavaScript it should be:

for (var b = 0; b < buttons.length; b++) {
    buttons[b].style.backgroundImage = "url('darkSquare.jpg')";
}

for (... in ...) actually iterates over all the "members" of an object

eg. using var x = {a: 1, b: 2, c: 3} in for(var m in x) console.log(m) will produce

> a
> b
> c

it kind of works with arrays because it considers the indices members like this:

var x = [1,2,3];
for (var m in x) console.log(m);
> 0
> 1
> 2

since it is giving you the indices as if they were members you can't distinguish. the pitfall is:

var x = [1,2,3];
x.stuff = 'boo!';
for (var m in x) console.log(m);
> 0
> 1
> 2
> stuff

General Rule of Thumb: only use for (... in ...) when iterating over members in an object, use for (var i = 0; i < array.length; i++) when using arrays

you can always cheat and use:

for (var i = 0, item = x[i]; i < x.length; item=x[++i]) {
    // now item is the current item
}
如若梦似彩虹 2025-01-15 06:28:10

您应该将循环更改为

for(var i = 0; i < buttons.length; i++)
{
   buttons[i].style.backgroundImage = "url('darkSquare.jpg')";
}​

您使用的循环在变量 b 中分配键。由于您没有使用 hasOwnProperty 函数,因此此循环还可能产生您可能不想要的其他数据。

您还可以像这样使用 for-in 循环

for(var b in buttons)
{
    if (buttons.hasOwnProperty(b))
        buttons[b].style.backgroundImage = "url('darkSquare.jpg')";
}​

You should change your loop as so

for(var i = 0; i < buttons.length; i++)
{
   buttons[i].style.backgroundImage = "url('darkSquare.jpg')";
}​

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(var b in buttons)
{
    if (buttons.hasOwnProperty(b))
        buttons[b].style.backgroundImage = "url('darkSquare.jpg')";
}​
混浊又暗下来 2025-01-15 06:28:10

使用 for ( ... in .... ) 时,第一个参数是数组中的键,因此您必须像这样使用它:

for( b in buttons ) {
    buttons[b].style.backgroundImage = "url('darkSquare.jpg')";
}

jsFiddle

when using for ( ... in .... ) the first parameter is the key in the array so you have to use it like this:

for( b in buttons ) {
    buttons[b].style.backgroundImage = "url('darkSquare.jpg')";
}

working example in jsFiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文