我知道标题有点混乱,但我不知道如何更好地解释它。 现在我有这样的:JS将事件添加到脚本中作为参数
<script>
button.onclick=delete;
</script>
但我需要发送由onclick事件产生的函数删除的事件。 喜欢的东西...
<script>
button.onclick=delete(event);
</script>
我不知道如何做到这一点。 请帮忙。 谢谢。
我知道标题有点混乱,但我不知道如何更好地解释它。 现在我有这样的:JS将事件添加到脚本中作为参数
<script>
button.onclick=delete;
</script>
但我需要发送由onclick事件产生的函数删除的事件。 喜欢的东西...
<script>
button.onclick=delete(event);
</script>
我不知道如何做到这一点。 请帮忙。 谢谢。
使用 '这' 指事件源关键字:
<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>
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>
后您的HTML – rory