JQuery如何触发元素事件
比如页面中有一个按钮:<input id="btn" type="button" value="提交" />
要触发它的点击事件可以有下面两种方式:
一,
直接获得元素,然后调用click方法就可以了,函数在事件里面写
二,
{
$("#btn1").bind("click",function(event){
alert("这是触发事件的第二种方式")
})
} )
这种方法是绑定事件
上面两种方式都可以,其中click可以换成其他的blur,focus等事件
如果是一个元素要触发两次事件,比如失去焦点,获取焦点,在js中要用到onfocus和onblur事件,在jquery中直接连续添加即可
$("#txtName").focus(function(){
$("#txtName").attr("value","");
}).blur(function(){
$("#txtName").addClass("aa");
});
})
或者
$(function(){
$("#txtName").focus(function(){
$(this).attr("value","");
}).blur(function(){
$(this).addClass("aa");
});
})
this代表当前元素
或者使用bind绑定两次事件都可以。
您可能感兴趣的文章:
jquery中的mouseleave和mouseout的区别 模仿下拉框效果
JQuery如何触发元素事件
jQuery Mobile 事件
JQuery live函数
鼠标移出事件的案例以及详解
jQuery 事件方法
jQuery文本框(input textare)事件绑定方法教程
HTML5中如何实现图片的拖放
jquery常用技巧及常用方法列表集合
关于jquery input textare 事件绑定及用法学习