如何在 jQuery 中实现自定义事件?
jqueryweb developmentfront end technology
自定义事件是指您可以在 jQuery 中创建自己的事件。例如,创建一个自定义事件,以便在按下键盘上的任意键时弹出警告框。
示例
您可以尝试运行以下代码来学习如何创建自定义事件。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('textarea').bind("enterKey",function(e){
alert("You have pressed Enter key!");
});
$('textarea').keyup(function(e){
if(e.keyCode == 13)
{
$(this).trigger("enterKey");
}
});
});
</script>
</head>
<body>
<textarea rows="2" cols="20">
</textarea>
</body>
</html>

