我需要修复这个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>
答案 0 :(得分:2)
使用 <{em> dot或bracket notation,而不是两者:
elem[i][actionIn] = function(){this.style.background='red';}
elem[i][actionOut] = function(){this.style.background='';}
答案 1 :(得分:1)
两个方括号对之间不需要.
。
elem[i][actionIn]
和elem[i][actionOut]
应该足够了。