Jquery 简明教程
jQuery - Utilities
Jquery 提供了一些 $(name space) 格式的实用程序。这些方法有助于完成编程任务。下面展示其中几个实用程序方法。
Jquery provides serveral utilities in the formate of $(name space). These methods are helpful to complete the programming tasks.a few of the utility methods are as show below.
$.trim()
$.trim() 用于移除前导和尾随空白
$.trim() is used to Removes leading and trailing whitespace
$.trim( " lots of extra whitespace " );
$.each()
$.each() 用于迭代数组和对象
$.each() is used to Iterates over arrays and objects
$.each([ "foo", "bar", "baz" ], function( idx, val ) {
console.log( "element " + idx + " is " + val );
});
$.each({ foo: "bar", baz: "bim" }, function( k, v ) {
console.log( k + " : " + v );
});
$.inArray()
$.inArray() 用于返回某个值在数组中的索引,如果数组中不存在该值,则返回 -1。
$.inArray() is used to Returns a value’s index in an array, or -1 if the value is not in the array.
var myArray = [ 1, 2, 3, 5 ];
if ( $.inArray( 4, myArray ) !== -1 ) {
console.log( "found it!" );
}
$.extend()
$.extend() 用于使用后续对象的属性更改第一个对象的属性。
$.extend() is used to Changes the properties of the first object using the properties of subsequent objects.
var firstObject = { foo: "bar", a: "b" };
var secondObject = { foo: "baz" };
var newObject = $.extend( firstObject, secondObject );
console.log( firstObject.foo );
console.log( newObject.foo );
$.proxy()
$.proxy() 用于返回一个总是在给定范围中运行的函数——即,将传入函数中的 this 的含义设置为第二个参数
$.proxy() is used to Returns a function that will always run in the provided scope — that is, sets the meaning of this inside the passed function to the second argument
var myFunction = function() {
console.log( this );
};
var myObject = {
foo: "bar"
};
myFunction(); // window
var myProxyFunction = $.proxy( myFunction, myObject );
myProxyFunction();
$.browser
$.browser 用于提供浏览器信息
$.browser is used to give the information about browsers
jQuery.each( jQuery.browser, function( i, val ) {
$( "<div>" + i + " : <span>" + val + "</span>" )
.appendTo( document.body );
});
$.contains()
$.contains() 用于判断由第二个参数提供的 DOM 元素是否是直接后代还是更深层嵌套后代的第一个参数提供的 DOM 元素的子级,返回 true。
$.contains() is used to returns true if the DOM element provided by the second argument is a descendant of the DOM element provided by the first argument, whether it is a direct child or nested more deeply.
$.contains( document.documentElement, document.body );
$.contains( document.body, document.documentElement );
$.data()
$.data() 用于提供数据信息
$.data() is used to give the information about data
<html lang = "en">
<head>
<title>jQuery.data demo</title>
<script src = "https://code.jquery.com/jquery-1.10.2.js">
</script>
</head>
<body>
<div>
The values stored were <span></span>
and <span></span>
</div>
<script>
var div = $( "div" )[ 0 ];
jQuery.data( div, "test", {
first: 25,
last: "tutorials"
});
$( "span:first" ).text( jQuery.data( div, "test" ).first );
$( "span:last" ).text( jQuery.data( div, "test" ).last );
</script>
</body>
</html>
输出如下
An output would be as follows
The values stored were 25 and tutorials
$.fn.extend()
$.fn.extend() 用于扩展 jQuery 原型
$.fn.extend() is used to extends the jQuery prototype
<html lang = "en">
<head>
<script src = "https://code.jquery.com/jquery-1.10.2.js">
</script>
</head>
<body>
<label><input type = "checkbox" name = "android">
Android</label>
<label><input type = "checkbox" name = "ios"> IOS</label>
<script>
jQuery.fn.extend({
check: function() {
return this.each(function() {
this.checked = true;
});
},
uncheck: function() {
return this.each(function() {
this.checked = false;
});
}
});
// Use the newly created .check() method
$( "input[type = 'checkbox']" ).check();
</script>
</body>
</html>
它提供以下所示的输出 −
It provides the output as shown below −
$.isWindow()
$.isWindow() 用于识别窗口
$.isWindow() is used to recognise the window
<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>jQuery.isWindow demo</title>
<script src = "https://code.jquery.com/jquery-1.10.2.js">
</script>
</head>
<body>
Is 'window' a window? <b></b>
<script>
$( "b" ).append( "" + $.isWindow( window ) );
</script>
</body>
</html>
它提供以下所示的输出 −
It provides the output as shown below −
$.now()
它返回一个代表当前时间数字
It returns a number which is representing the current time
(new Date).getTime()
$.isXMLDoc()
$.isXMLDoc() 检查文件是否是 xml
$.isXMLDoc() checks whether a file is an xml or not
jQuery.isXMLDoc( document )
jQuery.isXMLDoc( document.body )
$.globalEval()
$.globalEval() 用于全局执行 javascript
$.globalEval() is used to execute the javascript globally
function test() {
jQuery.globalEval( "var newVar = true;" )
}
test();
$.dequeue()
$.dequeue() 用于执行队列中的下一个函数
$.dequeue() is used to execute the next function in the queue
<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>jQuery.dequeue demo</title>
<style>
div {
margin: 3px;
width: 50px;
position: absolute;
height: 50px;
left: 10px;
top: 30px;
background-color: green;
border-radius: 50px;
}
div.red {
background-color: blue;
}
</style>
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<button>Start</button>
<div></div>
<script>
$( "button" ).click(function() {
$( "div" )
.animate({ left: '+ = 400px' }, 2000 )
.animate({ top: '0px' }, 600 )
.queue(function() {
$( this ).toggleClass( "red" );
$.dequeue( this );
})
.animate({ left:'10px', top:'30px' }, 700 );
});
</script>
</body>
</html>
它提供以下所示的输出 −
It provides the output as shown below −