Jquery事件链接
问题描述:
基于此问题
我不想丢弃等待点击事件和更改事件的准备好的东西一个鼠标悬停事件我希望在一个函数或事件中拥有所有这些东西。
I don't want to litter my ready stuff waiting for a "click event" AND a "change event" AND a "mouseover event" I want to have all that stuff in a single function or event.
可以将事件链接在一起。我不仅要捕获一个键盘,还要记录用户不在键盘上的点击或更改。
Is is possible to chain the events together. I want to capture not only a keyup, but also a click or a change in case the user isn't on the keyboard.
<script language="javascript" type="text/javascript">
$(document).ready( function () {
setMaxLength();
$("textarea.checkMax").keyup(function(){ checkMaxLength(this.id); } );
$("textarea.checkMax").mouseover(function(){ checkMaxLength(this.id); } );
});
</script>
此作品
$(document).ready( function () {
setMaxLength();
$("textarea.checkMax").bind("click mouseover keyup change", function(){checkMaxLength(this.id); } )
});
答
我认为你正在寻找绑定。绑定可以使用单个调用而不是使用链来将多个事件连接到同一个函数:
I think you are looking for bind. Bind can wire multiple events to the same function using a single call instead of using a chain:
$("textarea.checkMax").bind("keyup mouseover", checkMaxLength);