四:JavaScript处理
$.browser() 判断浏览器类型,返回boolen值
$(function(){
if($.browser.msie) {
alert("这是一个IE浏览器");}
else if($.browser.opera) {
alert("这是一个opera浏览器");}
})
当页面载入式判断浏览器类型,可判断的类型有msie、mozilla、opera、safari
$.each(obj, fn) obj为对象或数组,fn为在obj上依次执行的函数,注意区分$().each()
$.each( [0,1,2], function(i){ alert( "Item #" + i + ": " + this ); });
分别将0,1,2为参数,传入到function(i)中
$.each({ name: "John", lang: "JS" }, function(i){ alert( "Name: " + i + ", Value: " + this );
{ name: "John", lang: "JS" }为一个hash对象,依次将hash中每组对象传入到函数中
$.extend(obj, prop) 用第二个对象扩展第一个对象
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
$.extend(settings, options);
执行后settings对象为{ validate: true, limit: 5, name: "bar" }
可以用下面函数来测试
$(function(){
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
$.extend(settings, options);
$.each(settings, function(i){ alert( i + "=" + this ); });
})
$.grep(array,fn) 通过函数fn来过滤array,将array中的元素依次传给fn,fn必须返回一个boolen,如fn返回true,将被过滤
$(function(){
var arr= $.grep( [0,1,2,3,4], function(i){ return i > 2; });
$.each(arr, function(i){ alert(i); });
})
We can see that after executing $.grep, the array [0,1,2,3,4] becomes [0,1]
$.merge(first, second) Both parameters are arrays, sort out the ones in the second array that are the same as the first, and then merge the two arrays
$(function(){
> $.merge( [0,1,2 ], [2,3,4] ) .each(arr, function(i){ alert(i); });})
It can be seen that the result of arr is [0 ,1,2,3,4]$.trim(str)
Remove the spaces at both ends of the string
$.trim(" hello, how are you? ") results in "hello, how are you?"