Attribute operation and event introduction in jquery 02

jQuery special property manipulation

val method

The val method is used to set and get the value of form elements, such as the value of input and textarea

//Set the value 
$ ( "#name" ). val ( "Zhangsan" ); 
//Get the value 
$ ( "#name" ). val ();

[Case: Jingdong Search.html]

html method and text method

html method is equivalent to innerHTML text method is equivalent to innerText

//set content 
$ ( "div" ). html ( " < span > this is a piece of content < /span>"); 
//get content 
$ ( "div" ). html () 
​//
 set content 
$ ( " div" ). text ( " < span > this is a piece of content < /span>"); 
//get the content 
$ ( "div" ). text ()

Difference: The html method will recognize the html tag, the text method will directly treat the content as a string, and will not recognize the html tag.

width method and height method

set or get height


//With parameters, it means to set the height 
$ ( “img” ). height ( 200 ); 
//Get the height without parameters 
$ ( “img” ). height () 
;

Get the width and height of the viewable area of ​​the web page


//Get the width of the visible area 
$ ( window ) .width (); 
//Get the height of the visible area 
$ ( window ) .height ();

scrollTop与scrollLeft

Set or get the position of the vertical scroll bar


//Get the height of the page being curled 
$ ( window ). scrollTop (); 
// Get the width of the page being curled 
$ ( window ). scrollLeft ();

[Case: Case of imitating Tencent fixed menu bar] [Case: Case of small rocket returning to flight]

offset method and position method

offset方法获取元素距离document的位置,position方法获取的是元素距离有定位的父元素的位置。


//获取元素距离document的位置,返回值为对象:{left:100, top:100}
$(selector).offset();
//获取相对于其最近的有定位的父元素的位置。
$(selector).position();

jQuery事件机制

JavaScript中已经学习过了事件,但是jQuery对JavaScript事件进行了封装,增加并扩展了事件处理机制。jQuery不仅提供了更加优雅的事件处理语法,而且极大的增强了事件的处理能力。

jQuery事件发展历程(了解)

简单事件绑定>>bind事件绑定>>delegate事件绑定>>on事件绑定(推荐)

简单事件注册


click(handler)          单击事件
mouseenter(handler)     鼠标进入事件
mouseleave(handler)     鼠标离开事件

缺点:不能同时注册多个事件

bind方式注册事件


//第一个参数:事件类型
//第二个参数:事件处理程序
$("p").bind("click mouseenter", function(){
    //事件响应方法
});

缺点:不支持动态事件绑定

delegate注册委托事件

// 第一个参数:selector,要绑定事件的元素
// 第二个参数:事件类型
// 第三个参数:事件处理函数
$(".parentBox").delegate("p", "click", function(){
    //为 .parentBox下面的所有的p标签绑定事件
});

缺点:只能注册委托事件,因此注册时间需要记得方法太多了

on注册事件

on注册事件(重点)

jQuery1.7之后,jQuery用on统一了所有事件的处理方法。

最现代的方式,兼容zepto(移动端类似jQuery的一个库),强烈建议使用。

on注册简单事件


// 表示给$(selector)绑定事件,并且由自己触发,不支持动态绑定。
$(selector).on( "click", function() {});

on注册委托事件


// 表示给$(selector)绑定代理事件,当必须是它的内部元素span才能触发这个事件,支持动态绑定
$(selector).on( "click",“span”, function() {});

on注册事件的语法:


// 第一个参数:events,绑定事件的名称可以是由空格分隔的多个事件(标准事件或者自定义事件)
// 第二个参数:selector, 执行事件的后代元素(可选),如果没有后代元素,那么事件将有自己执行。
// 第三个参数:data,传递给处理函数的数据,事件触发的时候通过event.data来使用(不常使用)
// 第四个参数:handler,事件处理函数
$(selector).on(events[,selector][,data],handler);

事件解绑

unbind方式(不用)


$(selector).unbind(); //解绑所有的事件
$(selector).unbind("click"); //解绑指定的事件

undelegate方式(不用)


$( selector ).undelegate(); //解绑所有的delegate事件
$( selector).undelegate( “click” ); //解绑所有的click事件

off方式(推荐)


// 解绑匹配元素的所有事件
$(selector).off();
// 解绑匹配元素的所有click事件
$(selector).off("click");

触发事件


$(selector).click(); //触发 click事件
$(selector).trigger("click");

jQuery事件对象

jQuery事件对象其实就是js事件对象的一个封装,处理了兼容性。


//screenX和screenY   对应屏幕最左上角的值
//clientX和clientY   距离页面左上角的位置(忽视滚动条)
//pageX和pageY   距离页面最顶部的左上角的位置(会计算滚动条的距离)

//event.keyCode 按下的键盘代码
//event.data    存储绑定事件时传递的附加数据

//event.stopPropagation()   阻止事件冒泡行为
//event.preventDefault()    阻止浏览器默认行为
//return false:既能阻止事件冒泡,又能阻止浏览器默认行为。

【案例:钢琴版导航(加强).html】

jQuery补充知识点

链式编程

通常情况下,只有设置操作才能把链式编程延续下去。因为获取操作的时候,会返回获取到的相应的值,无法返回 jQuery对象。


end(); // 筛选选择器会改变jQuery对象的DOM对象,想要回复到上一次的状态,并且返回匹配元素之前的状态。

【案例:五角星评分案例.html】

each方法

jQuery的隐式迭代会对所有的DOM对象设置相同的值,但是如果我们需要给每一个对象设置不同的值的时候,就需要自己进行迭代了。

作用:遍历jQuery对象集合,为每个匹配的元素执行一个函数


// 参数一表示当前元素在所有匹配元素中的索引号
// 参数二表示当前元素(DOM对象)
$(selector).each(function(index,element){});

【案例:不同的透明度.html】

多库共存

jQuery使用作为标示符,但是如果与其他框架中的冲突时,jQuery可以释放$符的控制权.


var c = $.noConflict();//释放$的控制权,并且把$的能力给了c

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324773956&siteId=291194637