【问题标题】:pass argument to function using addEventListener使用 addEventListener 将参数传递给函数
【发布时间】:2014-05-25 04:50:52
【问题描述】:

我对 addEventListener 方法很陌生。我正在尝试使用 addEventListener 将参数传递给函数。但它不是 worining。为什么我不能使用 addEventListener 方法传递字符串“2a”?如果你这样做会很有帮助给我解释一下这个问题。谢谢

<html>
<head>
<style>
.mm{
width:100px;
height:60px;
}
</style>
</head>
<body>
<script>
function lister(){
document.getElementById("tab1").addEventListener("click",myfunc("2a"));
}
function myfunc(str){
alert(str);
}
window.onload=listener;
</script>
<table style="border:1px solid black;padding:2px;margin:2px 2px 2px 2px;">
<tr><td class="mm" id="tab1">this is it</td></tr>
<tr><td class="mm" id="tab2">no tis titt</td></tr>
</table>

</body>
</html>

【问题讨论】:

    标签: javascript


    【解决方案1】:

    唯一的办法就是使用匿名函数

    function lister(){
        document.getElementById("tab1").addEventListener("click", function() {
            myfunc("2a");
        }, false);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 2012-09-04
      • 2012-08-15
      • 2011-06-04
      • 2022-01-21
      • 2010-09-20
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多