我爱编程

JQuery & DOM事件

2017-01-11  本文已影响45人  jrg_memo

JQuery

bind() (已弃用)
1作用 向匹配元素添加一个或多个事件处理器
2方法 $(selector).bind(event,data,function)
3解除 $(selector).unbind(event,function)
live() (已弃用)
1作用 向匹配元素添加一个或多个事件处理器
2方法 $(selector).live(event,data,function)
3解除 $(selector).die(event,function)
delegate()
1 作用:
为(被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行
2 用法
单个事件:
$(selector).delegate(childSelector,event,data,function)
多个事件:
$(selector).delegate(childselector,{event1:function, event2:function, ...})
3 解除事件绑定:
$( selector ).undelegate(childselector, event, function );
4 版本
支持jquery 1.4.2+
on() (推荐使用)
1 作用:
为指定的元素,添加一个或多个事件处理程序,当这些事件发生时运行函数
2 用法
单个事件:
$(selector).on(event,childselector,data,function)
多个事件:
$(selector).on({event1:function, event2:function, ...},childselector);
3 解除事件绑定:
$( selector ).off(childselector, event, function );
4 版本
支持jquery 1.7+

hidespeed,callback) 隐藏元素
showspeed,callback)展示元素
togglespeed,callback)隐藏/展示元素 切换
可选参数speed:规定元素从隐藏到完全可见的速度,默认为0;
可选参数callback:show函数执行完了之后,要执行的函数;
工作原理:修改dom.style.display属性。
展示p1:
$("#btn1").on('click', function(){
$(".p1").show();
});
隐藏p1:
$("#btn2").on('click', function(){
$(".p1").hide();
});
切换:
$("#btn5").on('click', function(){
$(".p1").toggle();
});

.animate ({params},speed,callback);
必选参数params定义形成动画的CSS属性。
可选参数speed规定效果的时长。取值:“slow”/“fast”/毫秒
可选参数callback是动画完成后所执行的函数名称。
$("#btn14").on('click', function(e){
$div3.animate({
width: '200px'
}1000);
});

.html(null 获取 / string 设置) 获取或设置元素的HTML内容
.text(null 获取 / string 设置) 获取或设置元素的文本内容
<p id="test"> 这是段落中的<b>粗体</b>文本。</p>
获取
$('#test').html() ; // 这是段落中的<b>粗体</b>文本。
$('#test').text(); // 这是段落中的粗体文本
设置
$('#test').html('<span>hello</span>');
$('#test').text('hello');

1 **.val( ) ** 获取表单字段的值。
1.1 获取表单元素的值,如input selector textarea
1.2 获取匹配的元素集合 [0]的当前值,如表单有多个值;
2 .val( string OR array ) 设置表单字段的值。
2.1 设置表单元素的值为字符串;
2.2 设置表单元素的值为数组
3 **.val( function (index , value) ) ** 通过函数设置表单字段的值。
$('input:text.items').val(function(index,value){
retunrn value + ' ' + this.ClassName;
});

.atrr( 属性名) 获取匹配的元素集合 [0]的属性值
var $imgSrc=$("img").attr("src");
$("p").text("该图片地址为: "+$imgSrc);

上一篇 下一篇

猜你喜欢

热点阅读