使用addEventListener将参数传递给函数 [英] pass argument to function using addEventListener

查看:88
本文介绍了使用addEventListener将参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是addEventListener方法的新手。我正在尝试使用addEventListener将参数传递给函数。但它不是worining。为什么我不能使用addEventListener方法传递字符串2a?如果你将会非常有帮助向我解释这个问题。谢谢

i am quite new to addEventListener method.i am trying to pass argument to function using addEventListener .but it is not worining.why i can't passing the string "2a" using addEventListener method?it will be very helpful if you explain this problem to me.thanks

<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>


推荐答案

只有这样才能使用匿名函数

Only way to do that is to use an anonymous function

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

这篇关于使用addEventListener将参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆