2016-02-05 27 views
0

我知道标题有点混乱,但我不知道如何更好地解释它。 现在我有这样的:JS将事件添加到脚本中作为参数

<script> 
button.onclick=delete; 
</script> 

但我需要发送由onclick事件产生的函数删除的事件。 喜欢的东西...

<script> 
button.onclick=delete(event); 
</script> 

我不知道如何做到这一点。 请帮忙。 谢谢。

+0

后您的HTML – rory

回答

0

该事件已经传递给该函数。 jsFiddle

var button = document.getElementById('myButton'); 

button.onclick = func; 

function func(event) { 
    console.log(event); 
} 

你将不得不改变函数的名称,因为delete已经在JavaScript

0

使用 '这' 指事件源关键字:

<script> 
button.onclick=mydelete(this); 

//as noted by the other answer, delete is already a function, so choose your own name 
function mydelete(obj){ 
    //access properties of obj in here 
} 
</script> 

或jQuery的:

<script> 
$(button).click(function(){ 
    var obj=$(this); 
    //access obj properties here 
}); 
</script> 
0

Event是一个默认argument到处理程序。

function deleteFun(e){ 
 
    console.log(e) 
 
} 
 

 
document.getElementById("btn").onclick=deleteFun;
<button id="btn">click me</button>

如果您正在寻找传递自定义值,您可以使用.bind()的处理程序。

function notify(str){ 
 
    console.log(str) 
 
} 
 

 
document.getElementById("btn").onclick=notify.bind(null, "Hello World");
<button id="btn">click me</button>

相关问题