Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the applicable skills of jQuery

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today Xiaobian to share with you what jQuery application skills have related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some gains, let's learn about it together.

1. References to page elements

The element referenced by $() of jquery includes methods such as id, class, element name, hierarchical relationship of elements, dom or xpath condition, and the returned object is jquery object (collection object). The method defined by dom cannot be directly invoked.

2, jQuery object and dom object conversion

Only jquery objects can use methods defined by jquery. Note that dom objects and jquery objects are different. When calling methods, pay attention to whether the operation is dom objects or jquery objects.

Ordinary dom objects can generally be converted to jquery objects with $().

For example: $(document.getElementById("msg")) is a jquery object, you can use the jquery method.

Because the jquery object itself is a collection. So if jquery objects are to be converted to dom objects, one of them must be extracted, usually by index.

For example: $("#msg")[0],$("div").eq(1)[0],$("div").get()[1],$("td")[5] These are dom objects, you can use dom methods, but you can no longer use Jquery methods.

The following is correct:

$("#msg").html();

$("#msg")[0][xss_clean];

$("#msg").eq(0)[0][xss_clean];

$("#msg").get(0)[xss_clean];

3. How to get an item in jQuery collection

For the collection of elements obtained, obtaining one of them (specified by index) can be obtained using eq or get(n) method or index number. Note that eq returns jquery object, while get(n) and index return dom element object. For jquery objects, only jquery methods can be used, while dom objects can only use dom methods, such as to obtain the contents of the third element.

There are two ways:

$("div").eq(2).html(); //invoke methods of jquery objects

$("div").get(2)[xss_clean]; //invoke dom's method properties

The same function implements set and get

This is true of many methods in Jquery, including the following:

$("#msg").html(); //Returns the html content of the element node with id msg.

$("#msg").html("new content");

The requested URL//was not found on this server.

$("#msg").text(); //Returns the text content of the element node with id msg.

$("#msg").text("new content"); //Write "new content" as a normal text string into the element node content with id msg, and the page displays new content

$("#msg").height(); //Returns the height of the element with id msg

$("#msg").height("300"); //Set css height of elements with id msg to 300

$("#msg").width(); //Returns the css width of the element with id msg

$("#msg").width("300"); //Set width of element id msg to 300

$("input").val("); //Returns the value of the form input box

$("input").val("test"); //Set the value of the form input box to test

$("#msg").click(); //Trigger click event for element id msg

$("#msg").click(fn); //Add function for click-event for element id msg

Blur,focus,select,submit events can be invoked in two ways.

5. Collection processing function

Instead of looping through the contents of the collection returned by jquery and processing each object separately, jquery has provided us with a convenient way to process collections.

It comes in two forms:

$("p").each(function(i){this.style.color=['#f00','#0f0','#00f'][ i ]}) //Set different font colors for p elements with index 0, 1, 2 respectively.

$("tr").each(function(i){this.style.backgroundColor=['#ccc','#fff'][i%2]}) //Implement interlace effect for tables

$("p").click(function(){alert($(this).html()}) //adds click events for each p element, clicking on a p element pops up its contents

Expanding the functions we need

$.extend({min: function(a, b){return a

< b?a:b; }, max: function(a, b){return a >

b?a:b; } });

//为jquery扩展了min,max两个方法使用扩展的方法(通过"$.方法名"调用):

alert("a=10,b=20,max="+$.max(10,20)+",min="+$.min(10,20));

7、支持方法的连写

所谓连写,即可以对一个jquery对象连续调用各种不同的方法。

例如:

$("p").click(function(){alert($(this).html())})

.mouseover(function(){alert('mouse over event')})

.each(function(i){this.style.color=['#f00','#0f0','#00f'][ i ]});

8、操作元素的样式

主要包括以下几种方式:

$("#msg").css("background"); //返回元素的背景颜色

$("#msg").css("background","#ccc") //设定元素css背景为灰色

$("#msg").height(300); $("#msg").width("200"); //设定width和height

$("#msg").css({ color: "red", background: "blue" });//以名值对的形式设定样式

$("#msg").addClass("select"); //为元素增加名称为select的class

$("#msg").removeClass("select"); //删除元素名称为select的class

$("#msg").toggleClass("select"); //如果存在(不存在)就删除(添加)名称为select的class

9、完善的事件处理功能

Jquery已经为我们提供了各种事件处理方法,我们无需在html元素上直接写事件,而可以直接为通过jquery获取的对象添加事件。

如:

$("#msg").click(function(){alert("good")})

//为元素添加了单击事件

$("p").click(function(i){this.style.color=['#f00','#0f0','#00f'][ i ]})

//为三个不同的p元素单击事件分别设定不同的处理

jQuery中几个自定义的事件:

(1)hover(fn1,fn2):

一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法。当鼠标移动到一个匹配的元素上面时,会触发指定的第一个函数。当鼠标移出这个元素时,会触发指定的第二个函数。

//当鼠标放在html表格的某行上时将class置为over,离开时置为out。

$("tr").hover(function(){

$(this).addClass("over");},

function(){$(this).addClass("out");

});

(2)ready(fn):

当DOM载入就绪可以查询及操纵时绑定一个要执行的函数。

$(document).ready(function(){alert("Load Success")})

//页面加载完毕提示"Load Success",相当于onload事件。与$(fn)等价

(3)toggle(evenFn,oddFn):

每次点击时切换要调用的函数。

如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数。随后的每次点击都重复对这两个函数的轮番调用。//每次点击时轮换添加和删除名为selected的class。

$("p").toggle(function(){

$(this).addClass("selected");

},function(){

$(this).removeClass("selected");

});

(4)trigger(eventtype):

在每一个匹配的元素上触发某类事件。例如:

$("p").trigger("click"); //触发所有p元素的click事件

(5)bind(eventtype,fn),unbind(eventtype):

事件的绑定与反绑定从每一个匹配的元素中(添加)删除绑定的事件。

例如:

$("p").bind("click", function(){

alert($(this).text());

});

//为每个p元素添加单击事件

$("p").unbind(); //删除所有p元素上的所有事件

$("p").unbind("click") //删除所有p元素上的单击事件

10、几个实用特效功能

其中toggle()和slidetoggle()方法提供了状态切换功能。

如toggle()方法包括了hide()和show()方法。

slideToggle()方法包括了slideDown()和slideUp方法。

11、几个有用的jQuery方法

$.browser.浏览器类型:检测浏览器类型。有效参数:safari, opera, msie, mozilla。如检测是否ie:$.browser.isie,是ie浏览器则返回true。

$.each(obj, fn):通用的迭代函数。可用于近似地迭代对象和数组(代替循环)。

$.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n ); });

等价于:

var tempArr=[0,1,2];

for(var i=0;i 0 ? i + 1 : null; });

tempArr内容为:

[2,3]$.merge(arr1,arr2):合并两个数组并删除其中重复的项目。

如:

$.merge( [0,1,2], [2,3,4] ) //返回[0,1,2,3,4]

$.trim(str):删除字符串两端的空白字符。

如:

$.trim(" hello, how are you? "); //返回"hello,how are you? "

12、解决自定义方法或其他类库与jQuery的冲突

很多时候我们自己定义了$(id)方法来获取一个元素,或者其他的一些js类库如prototype也都定义了$方法,如果同时把这些内容放在一起就会引起变量方法定义冲突,Jquery对此专门提供了方法用于解决此问题。

使用jquery中的jQuery.noConflict();方法即可把变量$的控制权让渡给第一个实现它的那个库或之前自定义的$方法。之后应用Jquery的时候只要将所有的$换成jQuery即可,如原来引用对象方法$("#msg")改为jQuery("#msg")。

如:

jQuery.noConflict();

// 开始使用jQuery

jQuery("div p").hide();

// 使用其他库的 $()

$("content").style.display = 'none';

以上就是"jQuery适用技巧有哪些"这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report