Unexpected Token ILLEGAL on Javascript For Loop
I'm attempting to write a simple randomizer that will randomly set group's background position individually.
HTML:
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
<div class="button"></div>
CSS:
.button{
width: 100px;
height: 40px;
background:url(redMetal.jpg) no-repeat;
border: 1px solid #ff0000;
display: inline-block;
float: left;
margin: 20px;
text-align: center;
color: #fff;
}
Javascript:
var button = document.getElementsByClassName("button");
for (var i = 0; i < button.length; i++) {
x = Math.random() * (3499 - 0) + 0;
y = Math.random() * (2359 - 0) + 0;
button[i].style.backgroundPosition = (x * -1) + "px" + (y * -1) + "px";
button[i].innerHTML = i;
}
When I do this, I get an UNCAUGHT SYNTAXERROR: Unexpected token ILLEGAL right after the closing curly brace of the for loop, and I don't understand why.
You really have an illegal character after the last curly brace, see what i got copy-pasting that code on jsfiddle, notice the red dot.
button[i].innerHTML = i;
}*
Open that file with another editor, eg vim an check for weird characters with :set list
.
上一篇: javascript意外的非法令牌