Javascript错误后缺少名称。变量函数的运算符

时间:2013-04-23 14:51:53

标签: javascript html dom

我需要修复这个javascript错误我有“在变量后运算符后缺少名称”。这是我目前正在努力工作的代码。

    <!DOCTYPE html>
<html>
<body onload="myFunction()">

<select>
    <option>1</option>
</select>
<br/>
<select>
    <option>1</option>
</select>

<script>
function myFunction()
{
    if(typeof document.body.ontouchstart == "undefined"){actionIn = "onmouseover"; actionOut = "onmouseout"}
    else{actionIn = "ontouchstart"; actionOut = "ontouchend";}

    var elem = document.getElementsByTagName("SELECT");
    for (var i = 0;i < elem.length; i++){
        elem[i].[actionIn] = function(){this.style.background='red';}
        elem[i].[actionOut] = function(){this.style.background='';}
    }
}
</script>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

使用 <{em> dotbracket notation,而不是两者:

elem[i][actionIn] = function(){this.style.background='red';}
elem[i][actionOut] = function(){this.style.background='';}

答案 1 :(得分:1)

两个方括号对之间不需要.

elem[i][actionIn]elem[i][actionOut]

应该足够了。