将变量应用于javascript函数

时间:2013-04-23 14:22:04

标签: 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";}
    document.write(actionIn+" "+actionOut);
    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>

1 个答案:

答案 0 :(得分:3)

你正在尝试写

elem[i][actionIn] = ...;