【发布时间】:2014-07-16 20:37:15
【问题描述】:
我基于对一组名称的循环动态创建了一些按钮,然后我想在这些按钮上添加翻转操作,但是此代码中的 alert() 总是打印 last 的名称项目(黑色)。
我尝试在alert() 部分上使用eval(),但没有任何区别。
我希望它返回 红色、绿色或黑色,具体取决于我将鼠标悬停在哪个按钮上。
<div id="channels_buttons_container">
</div>
<script>
channels_array = ["red", "green", "black"];
for(var i = 0; i < channels_array.length; i++) {
loop_channel_name = channels_array[i];
// append an element inside the container
var new_button_element = document.createElement("span");
new_button_element.id = 'channel_button_'+loop_channel_name;
new_button_element.innerHTML = '<br>BLA BLA';
document.getElementById('channels_buttons_container').appendChild(new_button_element);
// try to add rollover actions on the new button
document.getElementById('channel_button_'+loop_channel_name).onmouseover = function(){
alert('Rollover '+loop_channel_name);
}
}
</script>
【问题讨论】:
标签: javascript html arrays loops eval