【问题标题】:Cannot properly create rollover on dynamically created html element无法在动态创建的 html 元素上正确创建翻转
【发布时间】: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


    【解决方案1】:

    代码没问题,但是当你使用“loop_channel_name”时,取数组的最后一个元素。您必须传递实际元素(this):

     document.getElementById('channel_button_'+loop_channel_name).onmouseover = function(){
    
            alert('Rollover '+this.id);
        }
    

    Example

    【讨论】:

    • 好的,这有帮助。我在 html 元素中设置了 channel_name 变量,如下所示new_button_element.channel_name = loop_channel_name;,然后由new_button_element.onmouseover = function(){alert(this.channel_name)} 引用它;这符合标准吗?像这样在html元素中设置变量?
    • 是的,在html5中为dom节点添加属性是有效的。 new_button_element["data-color"] = loop_channel_name;看例子:jsfiddle.net/48q9b/1
    【解决方案2】:

    loop_channel_name 被初始化为全局变量,因为您没有使用 var 关键字。试试var loop_channel_name 而不是loop_channel_name。当您在 for 循环的第一次迭代中初始化 loop_channel_name 时,您将其创建为全局变量,而在后续迭代中,您只是在更新它而不是创建新的引用。通过在警报中引用 loop_channel_name,您正在引用在循环的最后一次迭代中更新为 black 的全局变量,因此它总是提醒 black

    示例

    <div id="channels_buttons_container">
    </div>
    
    <script>
    
    channels_array = ["red", "green", "black"];
    
    for(var i = 0; i < channels_array.length; i++) {
    
        var loop_channel_name = channels_array[i];
    
        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);
    
        document.getElementById('channel_button_'+loop_channel_name).onmouseover = function(){
            alert('Rollover '+loop_channel_name);
            /*You could also do*/
            alert('Rollover '+ channels_array[i]);
        }
        // 
    
    }
    
    </script>
    

    【讨论】:

    • 它返回相同的结果(loop_channel_name的最后一个实例)和第二种方法(直接引用数组键)它每次返回undefined。但是我能够通过上面@cbertelegni 提到的方法解决这个问题,我发表了评论。我仍然不完全明白为什么我的原始代码不起作用。
    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2011-07-29
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多