Answer the question
In order to leave comments, you need to log in
What is the error in the script (JS)?
Good afternoon!
Question to those who know JavaScript
there is a script, I did not write
var img_id = new Array(1,1,2,3);<br/>
<br/>
var img_color = new Array(<br/>
'Brilliant 1',<br/>
'Brilliant 1',<br/>
'Brilliant 2',<br/>
'Brilliant 3'<br/>
);<br/>
<br/>
var img_url = new Array(<br/>
'color/1.jpg',<br/>
'color/1.jpg',<br/>
'color/2.jpg',<br/>
'color/3.jpg'<br/>
);<br/>
<br/>
var img_arr = new Array();<br/>
function preloadImages()<br/>
{<br/>
for (var id in img_url) {<br/>
img_arr[id] = new Image;<br/>
img_arr[id].src = img_url[id];<br/>
}<br/>
}<br/>
<br/>
preloadImages(); <br/>
<br/>
function setMainImage(id)<br/>
{ <br/>
if(id <= 50)<br/>
{<br/>
big = document.getElementById('main_image');<br/>
document.getElementById('login_response').innerHTML = img_color[id];<br/>
}<br/>
else<br/>
{<br/>
big = document.getElementById('main_image_ef');<br/>
document.getElementById('login_response_ef').innerHTML = img_color[id];<br/>
}<br/>
<br/>
bg = document.getElementById('i' + id);<br/>
if (big && bg)<br/>
{<br/>
var id_img = 0;<br/>
for (var index in img_id) {<br/>
if (img_id[index] == id) {<br/>
id_img = index;<br/>
}<br/>
}<br/>
big.src = img_arr[id_img].src;<br/>
}<br/>
return false;<br/>
}
Answer the question
In order to leave comments, you need to log in
This is all from the fact that js is written by a person who has not even read the initial level of documentation.
for (var id in img_url) - this construction iterates not only the elements of the array, but also all other properties of the object. Change to
for (var i=0; i<img_url.length; i++) {
var id = img_url[i];
....
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question