Home > Article > Web Front-end > Example parsing jQuery tool function
1. $.browser object properties
Property list A Mozilla Mozilla related browsers return True, otherwise False will return to True, such as Firefox
Safari Safari related browsers, otherwise returning to False, such as Safari
Opera Opera related browsers, otherwise, return to False, As Operas msie Msie related browsers return True, otherwise returning false, such as IE, 360, Sogou
Version Return to the corresponding browser version
R$(function () { if ($.browser.msie) { alert("IE浏览器"); } if ($.browser.webkit) { alert("webkit浏览器"); } if ($.browser.mozilla) { alert("mozilla浏览器"); } if ($.browser.safari) { alert("safari浏览器"); } if ($.browser.opera) { alert("opera浏览器"); } alert($.browser.version); })E 2. Boxmodel return a Boolean value, if it is W3C If the box model is used, it returns true, otherwise it returns false.
There are two types of box models, one is the W3C box model and the other is the IE box model. The fundamental difference between the two is that the W3C box model does not include padding and border, but only refers to the Height and Width of the content, while the IE box model includes padding and border.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="jQuery.1.8.3.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { if ($.support.boxModel) { alert("W3C盒子模型!"); } else { alert("IE盒子模型!"); } }) </script> </head> <body> </body> </html>
The above example pops up the W3C box model. If the top two lines are deleted, d1c22ceb26a2da9255b48cb0b4ff90cd 68ccb177a5de0ef9542dde7d35bae727. What pops up is the IE box model. Array and object operations
3. $.each()
This tool function can not only complete the traversal of the specified array, but also achieve the traversal of elements in the page.
Grammar: $.each(obj,fn(para1,para2)) obj is the array or object to be traversed, fn is the callback function executed for each traversed element, para1 represents the serial number of the array or the attribute of the object, and para2 represents the Properties of elements and objects.
$(function () { var arr = [1, 2, 3, 4, 5]; $.each(arr, function (index, value) { document.write(index + ":"); document.write(value + "<br/>"); }); }) 输出: 0:1 1:2 2:3 3:4 4:5 $.each()遍历数组。
$(function () { var arr = { "张三": "23","李四": 22,"王五": "21" }; $.each(arr, function (index, value) { document.write(index + ":"); document.write(value + "<br/>"); }); }) 输出:张三:23 李四:22 王五:21
Element traversal
<head> <title></title> <script src="jQuery.1.8.3.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("p").each(function () { $(this).css("background-color", "red"); }); //一下三行代码与以上三行效果一样 //$.each($("p"), function () { // $(this).css("background-color", "red"); //}) }) </script> </head> <body> <p>我是第一个P</p> <p>我是第二个P</p> <p>我是第三个P</p> <p>我是第四个P</p> <p>我是第五个P</p> </body> </html>
Syntax :$.grep(Arrar,fn(value ,index)); Pay attention to the order of the parameters of the callback function. The first one is the value, and the second one is the index.
$.grep(Arrar,fn(value,index),[bool]); The third parameter indicates whether to invert, true means to invert, false means not to invert. If If the searched element exists in the array, the index of the searched element will be returned.
$(function () { var arr = [2, 5, 34, 22, 8]; var arr1 = $.grep(arr, function(value, index) { return index <= 2 && value < 10; }) document.write(arr1.join()); //输出2,5 })
$.isArray(obj) Detect whether the parameter is an array $.isFunction(obj) Detect whether the parameter is a function $.isEmptyObject(obj) Detect whether the parameter is an empty object $.isPlainObject(obj) Detect Whether the parameter is a pure object, that is, whether the object is created through {} or new Object() keyword.
$.contains(container,contained) Detect whether a DOM node contains another DOM node. If yes, it returns true otherwise it means false. Note that the parameter is a DOM object, not a jQuery object.
$(function () { var arr = [2, 5, 34, 22, 8]; var arr1 = $.map(arr, function (value, index) { if (value > 5 && index < 3) { return value - 10; } }) document.write(arr.join() + "<br/>"); //2,5,34,22,8 可以看到原数组不改变 document.write(arr1.join()); //24 新数组只获得了操作之后的结果 })
10. $.param()
Serialized into url string
$(function () { var arr = [1, 2, 3, 4, 5]; alert($.inArray(4,arr)); //弹出 3 })
11. $.makeArray()
Copy the properties of the array or array-like object to a new array (really an array) and return the new array.
$(function () { var str = " 你在他乡还好吗? "; document.write("11" + str + "11" + "<br/>"); //输出 11 你在他乡还好吗? 11 document.write("11" + $.trim(str) + "11"); //输出 11你在他乡还好吗?11 //加个11是为了看清楚差别。 })
12. $.merge()
This function accepts two arrays or array-like objects, appends the second parameter to the first parameter, returns the first parameter, and the first The first array will be modified, but the second one will not.
$(function () { var arr = [1, 2, 3, 2, 1]; document.write(jQuery.isArray(arr)); //返回true var str = "123"; document.write(jQuery.isArray(str)); //返回false }) $(function () { var f = fun1; alert($.isFunction(fun1)); //返回true }) function fun1() { } $(function () { var obj1 = {}; var obj2 = { name: "张飞" }; alert($.isEmptyObject(obj1)); //返回true obj1是空对象 alert($.isEmptyObject(obj2)); //返回false obj2不是空对象 }) $(function () { var obj1 = {}; var obj2 = { name: "张飞" }; var obj3 = new Object(); var obj4 = null; alert($.isPlainObject(obj1)); //true 通过{}创建 alert($.isPlainObject(obj2)); //true 通过{}创建 alert($.isPlainObject(obj3)); //true 通过new Object()创建 alert($.isPlainObject(obj4)); //flase 不是通过{}或new Object()创建 }) $(function () { alert($.contains($("#div1")[0],$("#p1")[0])); //返回true,注意参数是DOM对象,并非jQuery对象 })
13. $.parseJSON()This function will parse the string in JSON format and return the parsing result (object). Similar to JSON.parse(), note: jQuery only defines a JSON parsing function, not a serialization function.
$(function () { var man = { Name: "张飞", Age: 23 }; var str = $.param(man); document.write(str); //Name=%E5%BC%A0%E9%A3%9E&Age=23 var str1 = decodeURI(str); document.write("<br>" + str1); //Name=张飞&Age=23 })
var arr = [1,3,5,7,9]; $(function () { var arr1 = $.makeArray(arr); document.write(arr1.join()); //输出 1,3,5,7,9 })
Fifteen, $.unique(array)
Delete duplicate elements in the element array
var arr1 = [1, 3, 5, 7, 9]; var arr2 = [2, 4, 6, 8, 10]; $(function () { var arr3 = $.merge(arr1, arr2); document.write(arr1.join() + "<br/>"); //1,3,5,7,9,2,4,6,8,10 document.write(arr2.join() + "<br/>"); //2,4,6,8,10 document.write(arr3.join() + "<br/>"); //1,3,5,7,9,2,4,6,8,10 })
省略dest参数,extend方法原型中的dest参数是可以省略的,如果省略了,则该方法就只能有一个src参数,而且是将该src合并到调用extend方法的对象中去。
要特别注意的一点是:后面的值会覆盖前面同名的值。
$(function(){ $.extend({ hello:function(){alert('hello');} //该方法只有一个参数,意味着将hello方法合并到jQuery全局对象中去 }); $.hello(); //弹出 hello })
命名空间示例:
$(function(){ $.extend({net:{}}); //扩展一个命名空间 $.extend($.net,{ hello:function(){alert('hello');} //将hello方法绑定到命名空间net里去 }) $.net.hello(); //通过net命名空间调用方法 })
拷贝方法原型:
extend(boolean,dest,src1,src2,src3...)
其中第一个参数boolean表示是否进行深层拷贝。
$(function(){ var result=$.extend( true, {}, { name: "John", location: {city: "Boston",country:"USA"} }, { last: "Resig", location: {state: "MA",country:"China"} } ); alert(result.location.state); //输出 MA //result={name:"John",last:"Resig", location:{city:"Boston",state:"MA",county:"China"}} var result=$.extend( false, {}, { name: "John", location: {city: "Boston",country:"USA"} }, { last: "Resig", location: {state: "MA",country:"China"} } ); alert(result.location.city); //输出 undefined //result={name:"John",last:"Resig",location:{state:"MA",county:"China"}} 注意没有city,只是合并了location,location里面的属性不管 })