From: 011netservice@gmail.com
Date: 2023-01-02
Subject: readme-GitHub.txt
File: CodeHelper\JavaScript\jQuery\KeyWord-jQuery.txt
歡迎來信交流, 訂購軟體需求.
以下
#### 標記段落,
**** 標記常用(流程、設定、備忘、常見問題)
#### 常用案例
**** 2023-01-02 jQuery ajax
#### 2023-01-02, 以下舊資料確認後移到上面
1. jquery.min.js is a compressed version of jquery.js
2. slim version doesn't support ajax.
3. jquery-ui 不包含在 (jquery 或 jquery.slim 中), 各自獨立.
Template:
/CodeHelper/JavaScript/jQuery/KeyWord/Template-UI-20220404.htm
CDN:
https://releases.jquery.com/
CDN UI:
base themes: https://code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css
black-tie themes: https://code.jquery.com/ui/1.13.1/themes/black-tie/jquery-ui.css
其餘的 themes 到 https://releases.jquery.com/ 取得
CDN mobile
jQuery Mobile theme: https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css
jQuery Mobile structure: https://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css
----------
20210829
select an item using class or ID
$("#myDivId"); // Selecting Elements by ID.
$("#myCssClass"); // Selecting Elements by Class Name
var myJavaScriptVariable = $( "#myDivId" ); // 轉成 JavaScript Variable.
$( "input[name='first_name']" ); // Selecting Elements by Attribute.
$( "#contents ul.people li" ); // Selecting Elements by Compound CSS Selector
$( "div.myClass, ul.people" ); // Selecting Elements with a Comma-separated List of Selectors
Get/Set of a form input.
var myValue = $( "#myDivId" ).val(); // Get the value of a form input.
$( "#myDivId" ).val( "hello world" ); // Set the value of a form input.
Pseudo-Selectors:
$( "a.external:first" );
$( "tr:odd" );
// Select all input-like elements in a form (more on this below).
$( "#myForm :input" );
$( "div:visible" );
// All except the first three divs.
$( "div:gt(2)" );
// All currently animated divs.
$( "div:animated" );
The best way to determine if there are any elements is to test the selection's .length property, which tells you how many elements were selected. If the answer is 0, the .length property will evaluate to false when used as a boolean value:
// Testing whether a selection contains elements.
if ( $( "div.foo" ).length ) { // 檢查 Selection 結果是否有元素?
...
}
----------
20210829
Types:
https://api.jquery.com/Types/
jQuery 使用到的 Types.
This page documents data types appearing in jQuery function signatures, whether defined by JavaScript itself or further restricted by jQuery. Unless explicitly stated otherwise, jQuery functions require primitive values where applicable, and do not accept their Object-wrapped forms. If you want to study these concepts in depth, take a look at MDN.
You should be able to try out most of the examples below by just copying them to your browser's JavaScript Console.
Whenever an example mentions that a type defaults to a boolean value, the result is good to know when using that type in a boolean context:
var x = "";
if ( x ) {
console.log( "x defaulted to true" );
} else {
console.log( "x defaulted to false" );
}
In this case, "x defaulted to false" is printed.
To keep the examples short, the invert ("not") operator and double-negation are used to show a boolean context:
var x = "";
!x // true
!!x // false (Double negation: Since "not (empty string)" is true, negating that makes it false)
Anything
The Anything virtual type is used in jQuery documentation to indicate that any type can be used or should be expected.
String
A string in JavaScript is an immutable primitive value that contains none, one or many characters.
(number 跟 string) 均為不可變的原始值 immutable primitive values.
"I'm a String in JavaScript!"
'So am I!'
The type of a string is "string".
typeof "some string"; // "string"
Quoting
A string can be defined using single or double quotes. You can nest single quotes inside of double quotes, and the other way around. To mix double quotes with double quotes (or single with single), the nested ones have to be escaped with a backslash.
"You make 'me' sad." // 雙引號夾住單引號.
'That\'s "cranking" good fun!' // 單引號夾住雙引號.
"Home"
Built-in Methods
A string in JavaScript has some built-in methods to manipulate the string, though the result is always a new string - or something else, eg. split returns an array.
"hello".charAt( 0 ) // "h"
"hello".toUpperCase() // "HELLO"
"Hello".toLowerCase() // "hello"
"hello".replace( /e|o/g, "x" ) // "hxllx", regexp 運算, 將 e 或 o 全部改成 x.
"1,2,3".split( "," ) // [ "1", "2", "3" ]
Length Property
All strings have a length property.
"Hello".length // 5
"".length // 0
Boolean Default
An empty string defaults to false:
!"" // true, string 空字串""為 false.
!!"" // false
!"hello" // false
!"true" // false
!new Boolean( false ) // false
htmlString
A string is designated htmlString in jQuery documentation when it is used to represent one or more DOM elements, typically to be created and inserted in the document. When passed as an argument of the jQuery() function, the string is identified as HTML if it starts with ) and is parsed as such until the final > character. Prior to jQuery 1.9, a string was considered to be HTML if it contained anywhere within the string.
When a string is passed as an argument to a manipulation method such as .append(), it is always considered to be HTML since jQuery's other common interpretation of a string (CSS selectors) does not apply in those contexts.
For explicit parsing of a string to HTML, the $.parseHTML() method is available as of jQuery 1.8.
// Appends hello:
$( "hello" ).appendTo( "body" ); // 添加(htmlString字串)到 "body" 之後.
// Appends hello:
$( "hellobye" ).appendTo( "body" ); // 添加(htmlString字串)到 "body" 之後, 本案例不會加入 "bye".
// Syntax error, unrecognized expression: byehello
$( "byehello" ).appendTo( "body" ); // 添加(htmlString字串)到 "body" 之後, 本案例無法辨識.
// Appends byehello:
$( $.parseHTML( "byehello" ) ).appendTo( "body" ); // 添加(htmlString字串)到 "body" 之後, 本案例讀取無法辨識字串中的(htmlString字串)部份.
// Appends hellowaitbye:
$( "hellowaitbye" ).appendTo( "body" ); // 添加(htmlString字串)到 "body" 之後, 本案例前後包夾無法辨識的字串.
Number
Numbers in JavaScript are double-precision 64-bit format IEEE 754 values. They are immutable primitive values, just like strings. All operators common in c-based languages are available to work with numbers (+, -, *, /, %, =, +=, -=, *=, /=, ++, --).
JavaScript 的 number 型別為 8 Bytes 的 double-precision 64-bit format IEEE 754 values.
(number 跟 string) 均為不可變的原始值 immutable primitive values.
12
3.543
The type of a number is "number".
typeof 12 // "number"
typeof 3.543 // "number"
Boolean Default
If a number is zero, it defaults to false:
!0 // true, number 0 為 false
!!0 // false
!1 // false
!-1 // false
Due to the implementation of numbers as double-precision values, the following result is not an error:
0.1 + 0.2 // 0.30000000000000004, 由於 number 為 double-precision, 所以計算結果會有誤差.
Math
JavaScript provides utilities to work with numbers in the Math object:
Math.PI // 3.141592653589793, number 可使用 Math 運算功能.
Math.cos( Math.PI ) // -1, number 可使用 Math 運算功能.
Parsing Numbers, 字串轉成數值, convert string to number.
parseInt and parseFloat help parsing strings into numbers. Both do some implicit conversion if the base isn't specified:
parseInt( "123" ) = 123 // (implicit decimal), 字串(10進位)轉成整數. convert string to number.
parseInt( "010" ) = 8 // (implicit octal), 字串( 8進位)轉成整數. convert string to number.
parseInt( "0xCAFE" ) = 51966 // (implicit hexadecimal), 字串(16進位)轉成整數. convert string to number.
parseInt( "010", 10 ) = 10 // (explicit decimal), 字串(10進位)轉成整數. convert string to number.
parseInt( "11", 2 ) = 3 // (explicit binary), 字串( 2進位)轉成整數. convert string to number.
parseFloat( "10.10" ) = 10.1 // (explicit binary), 字串( 2進位)轉成浮點數. convert string to number.
Numbers to Strings
When appending numbers to string, the result is always a string. The operator is the same, so be careful: If you want to add numbers and then append them to a string, put parentheses around the numbers:
number 加到 string 的結果為 string 型別.
number 加到 string 的結果為 string 型別, 可利用() 區隔出(數值運算)跟(字串結合).
"" + 1 + 2; // "12", number 加到 string 的結果為 string 型別.
"" + ( 1 + 2 ); // "3", number 加到 string 的結果為 string 型別, 可利用() 區隔出(數值運算)跟(字串結合).
"" + 0.0000001; // "1e-7", 浮點數值轉為字串.
parseInt( 0.0000001 ); // 1 (!), 這個要避免.
Or you use the String class provided by javascript, which try to parse a value as string:
String( 1 ) + String( 2 ); // "12", 使用 String() 可明確將數值轉為字串.
String( 1 + 2 ); // "3", 使用 String() 可明確將數值轉為字串.
NaN and Infinity
Parsing something that isn't a number results in NaN. isNaN helps to detect those cases:
parseInt( "hello", 10 ) // NaN, 錯誤字串(10進位)轉成整數結果為 NaN. convert string to number.
isNaN( parseInt("hello", 10) ) // true, 錯誤字串(10進位)轉成整數結果為 NaN. 可用 isNaN() 偵測轉換結果. convert string to number.
Division by zero results in Infinity: ???????????
1 / 0 // Infinity
Both NaN and Infinity are of type "number":
typeof NaN // "number"
typeof Infinity // "number"
Note that NaN compares in a strange way:
NaN === NaN // false (!)
But:
Infinity === Infinity // true
Integer
An integer is a plain Number type, but whenever explicitly mentioned, indicates that a non-floating-point number is expected.
Float
A float is a plain Number type, just as Integer, but whenever explicitly mentioned, indicates that a floating-point number is expected.
Boolean
A boolean in JavaScript can be either true or false:
if ( true ) console.log( "always!" );
if ( false ) console.log( "never!" );
Object
Everything in JavaScript is an object, though some are more objective (haha). The easiest way to create an object is the object literal:
var x = {};
var y = {
name: "Pete",
age: 15
};
The type of an object is "object":
typeof {} // "object"
Dot Notation
You can write and read properties of an object using the dot notation:
y.name // "Pete"
y.age // 15
x.name = y.name + " Pan" // "Pete Pan"
x.age = y.age + 1 // 16
Array Notation
Or you write and read properties using the array notation, which allows you to dynamically choose the property:
var operations = {
increase: "++",
decrease: "--"
};
var operation = "increase";
operations[ operation ] // "++"
operations[ "multiply" ] = "*"; // "*"
Iteration
Iterating over objects is easy with the for-in-loop:
var obj = {
name: "Pete",
age: 15
};
for( key in obj ) {
alert( "key is " + [ key ] + ", value is " + obj[ key ] );
}
Note that for-in-loop can be spoiled by extending Object.prototype (see Object.prototype is verboten) so take care when using other libraries.
jQuery provides a generic each function to iterate over properties of objects, as well as elements of arrays:
jQuery.each( obj, function( key, value ) {
console.log( "key", key, "value", value );
});
The drawback is that the callback is called in the context of each value and you therefore lose the context of your own object if applicable. More on this below at Functions.
Boolean default
An object, no matter if it has properties or not, never defaults to false:
!{} // false
!!{} // true
Prototype
All objects have a prototype property. Whenever the interpreter looks for a property, it also checks in the object's prototype if the property is not found on the object itself. jQuery uses the prototype extensively to add methods to jQuery instances. Internally, jQuery makes jQuery.fn an alias of jQuery.prototype so you can use either one (though plugin developers have standardized on fn).
var form = $("#myform");
console.log( form.clearForm ); // undefined
// jQuery.fn === jQuery.prototype
jQuery.fn.clearForm = function() {
return this.find( ":input" ).each(function() {
this.value = "";
}).end();
};
// works for all instances of jQuery objects, because
// the new method was added to the prototype
console.log( form.clearForm ); // function
form.clearForm();
Array
Arrays in JavaScript are mutable lists with a few built-in methods. You can define arrays using the array literal:
var x = []; // 定義 Array
var y = [ 1, 2, 3 ]; // 定義 Array
The type of an array is "object":
typeof []; // "object", typeof 為 "object".
typeof [ 1, 2, 3 ]; // "object", typeof 為 "object"
Reading and writing elements to an array uses the array-notation:
x[ 0 ] = 1; // 以 array-notation 存取陣列元素.
y[ 2 ] // 3
Iteration
An array has a length property that is useful for iteration:
for ( var i = 0; i < a.length; i++ ) { // array.length 可檢查元素個數.
// Do something with a[i]
}
When performance is critical, reading the length property only once can help to speed things up. This should be used only when a performance bottleneck was discovered:
for ( var i = 0, j = a.length; i < j; i++ ) { // 限制存取範圍可增加效能.
// Do something with a[i]
}
Another variation defines a variable that is filled for each iteration, removing the array-notation from the loop-body. It does not work when the array contains 0 or empty strings!
for ( var i = 0, item; item = a[i]; i++ ) { // 這方法不適用(有存放0或空字串的元素), 也就是過濾出(非0且非空字串)的元素清單.
// Do something with item
}
jQuery provides a generic each function to iterate over element of arrays, as well as properties of objects:
var x = [ 1, 2, 3 ];
jQuery.each( x, function( index, value ) { // jQuery 通用的 each() 方式.
console.log( "index", index, "value", value );
});
The drawback is that the callback is called in the context of each value and you therefore lose the context of your own object if applicable. More on this below at Functions.
The length property can also be used to add elements to the end of an array. That is equivalent to using the push-method:
var x = [];
x.push( 1 ); // array.length 可用來控制增加元素, 功能同 array.push().
x[ x.length ] = 2; // array.length 可用來控制增加元素, 功能同 array.push().
x // [ 1, 2 ]
You'll see both variations a lot when looking through JavaScript library code.
Other built-in methods are reverse, join, shift, unshift, pop, slice, splice and sort:
var x = [ 0, 3, 1, 2 ];
x.reverse() // [ 2, 1, 3, 0 ] 反轉
x.join(" – ") // "2 - 1 - 3 - 0" 連接
x.pop() // [ 2, 1, 3 ] 刪除最後一個元素.
x.unshift( -1 ) // [ -1, 2, 1, 3 ] 插入最前面一個元素.
x.shift() // [ 2, 1, 3 ] 刪除最前面一個元素.
x.sort() // [ 1, 2, 3 ] 排序
x.splice( 1, 2 ) // [ 2, 3 ] 拼接, splice(start[, deleteCount[, item1[, item2[, ...]]]]), 沒事別亂用!
Note: .unshift() method does not return a length property in Internet Explorer.
Boolean Default
An array, no matter if it has elements or not, never defaults to false:
![] // false, Array 不管是否有存放元素否, 預設為 true.
!![] // true, Array 不管是否有存放元素否, 預設為 true.
Array Notation
In the jQuery API you'll often find the notation of Array:
dragPrevention Array // Array 可指定只存放型別.
This indicates that the method doesn't only expect an array as the argument, but also specifies the expected type. The notation is borrowed from Java 5's generics notation (or C++ templates).
Array-Like Object
Either a true JavaScript Array or a JavaScript Object that contains a nonnegative integer length property and index properties from 0 up to length - 1. This latter case includes array-like objects commonly encountered in web-based code such as the arguments object and the NodeList object returned by many DOM methods.
When a jQuery API accepts either plain Objects or Array-Like objects, a plain Object with a numeric length property will trigger the Array-Like behavior.
PlainObject
The PlainObject type is a JavaScript object containing zero or more key-value pairs. The plain object is, in other words, an Object object. It is designated "plain" in jQuery documentation to distinguish it from other kinds of JavaScript objects: for example, null, user-defined arrays, and host objects such as document, all of which have a typeof value of "object." The jQuery.isPlainObject() method identifies whether the passed argument is a plain object or not, as demonstrated below:
var a = [];
var d = document;
var o = {};
typeof a; // object
typeof d; // object
typeof o; // object
jQuery.isPlainObject( a ); // false
jQuery.isPlainObject( d ); // false
jQuery.isPlainObject( o ); // true
Null
The null keyword is a JavaScript literal that is commonly used to express the absence of an intentional value.
Date
The Date type is a JavaScript object that represents a single moment in time. Date objects are instantiated using their constructor function, which by default creates an object that represents the current date and time.
new Date();
To create a Date object for an alternative date and time, pass numeric arguments in the following order: year, month, day, hour, minute, second, millisecond — although note that the month is zero-based, whereas the other arguments are one-based. The following creates a Date object representing January 1st, 2014, at 8:15.
new Date( 2014, 0, 1, 8, 15 );
Function
A function in JavaScript can be either named or anonymous. Any function can be assigned to a variable or passed to a method, but passing member functions this way can cause them to be called in the context of another object (i.e. with a different "this" object).
function named() {}
var handler = function() {}
You see a lot of anonymous functions in jQuery code:
$( document ).ready(function() {});
$( "a" ).click(function() {});
$.ajax({
url: "someurl.php",
success: function() {}
});
The type of a function is "function".
Arguments
Inside a function a special variable "arguments" is always available. It's similar to an array in that it has a length property, but it lacks the built-in methods of an array. The elements of the pseudo-array are the argument of the function call.
function log( x ) {
console.log( typeof x, arguments.length );
}
log(); // "undefined", 0
log( 1 ); // "number", 1
log( "1", "2", "3" ); // "string", 3
The arguments object also has a callee property, which refers to the function you're inside of. For instance:
var awesome = function() { return arguments.callee; }
awesome() === awesome // true
Context, Call and Apply
In JavaScript, the variable "this" always refers to the current context. By default, "this" refers to the window object. Within a function this context can change, depending on how the function is called.
All event handlers in jQuery are called with the handling element as the context.
$( document ).ready(function() {
// this refers to window.document
});
$( "a" ).click(function() {
// this refers to an anchor DOM element
});
You can specify the context for a function call using the function-built-in methods call and apply. The difference between them is how they pass arguments. Call passes all arguments through as arguments to the function, while apply accepts an array as the arguments.
function scope() {
console.log( this, arguments.length );
}
scope() // window, 0
scope.call( "foobar", [ 1, 2 ] ); // "foobar", 1
scope.apply( "foobar", [ 1, 2 ] ); // "foobar", 2
Scope
In JavaScript, all variables defined inside a function are only visible inside that function scope. Consider the following example:
// global
var x = 0;
(function() {
// private
var x = 1;
console.log( x ); // 1
})();
console.log( x ); // 0
It defines a variable x in the global scope, then defines an anonymous function and executes it immediately (the additional parentheses are required for immediate execution). Inside the function another variable x is defined with a different value. It is only visible within that function and doesn't overwrite the global variable.
Closures
Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope. In the following example, the variable counter is visible within the create, increment, and print functions, but not outside of them.
function create() {
var counter = 0;
return {
increment: function() {
counter++;
},
print: function() {
console.log( counter );
}
}
}
var c = create();
c.increment();
c.print(); // 1
The pattern allows you to create objects with methods that operate on data that isn't visible to the outside—the very basis of object-oriented programming.
Proxy Pattern
Combining the above knowledge gives you as a JavaScript developer quite a lot of power. One way to combine that is to implement a proxy pattern in JavaScript, enabling the basics of aspect-oriented programming (AOP):
(function() {
// log all calls to setArray
var proxied = jQuery.fn.setArray;
jQuery.fn.setArray = function() {
console.log( this, arguments );
return proxied.apply( this, arguments );
};
})();
The above wraps its code in a function to hide the "proxied"-variable. It saves jQuery's setArray-method in a closure and overwrites it. The proxy then logs all calls to the method and delegates the call to the original. Using apply(this, arguments) guarantees that the caller won't be able to notice the difference between the original and the proxied method.
Callback
A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered. jQuery's event system uses such callbacks everywhere:
$( "body" ).click(function( event ) {
console.log( "clicked: " + event.target );
});
Most callbacks provide arguments and a context. In the event-handler example, the callback is called with one argument, an Event. The context is set to the handling element, in the above example, document.body.
Some callbacks are required to return something, others make that return value optional. To prevent a form submission, a submit event handler can return false:
$( "#myform" ).submit(function() {
return false;
});
Instead of always returning false, the callback could check fields of the form for validity, and return false only when the form is invalid.
Error
An instance of an Error object is thrown as an exception when a runtime error occurs. Error can also be used as base to define user custom exception classes. In JavaScript an error can be thrown as shown below:
throw new Error( "The argument provided is incorrect" );
An error can also be thrown by the engine under some circumstances. For example, when trying to access a property of null:
var obj = null;
console.log( obj.foo() );
Selector
A selector is used in jQuery to select DOM elements from a DOM document. That document is, in most cases, the DOM document present in all browsers, but can also be an XML document received via Ajax.
The selectors are a composition of CSS and custom additions. All selectors available in jQuery are documented on the Selectors API page.
There are lot of plugins that leverage jQuery's selectors in other ways. The validation plugin accepts a selector to specify a dependency, whether an input is required or not:
emailrules: {
required: "#email:filled"
}
This would make a checkbox with name "emailrules" required only if the user entered an email address in the email field, selected via its id, filtered via a custom selector ":filled" that the validation plugin provides.
If Selector is specified as the type of an argument, it accepts everything that the jQuery constructor accepts, eg. Strings, Elements, Lists of Elements.
Event
jQuery's event system normalizes the event object according to W3C standards. The event object is guaranteed to be passed to the event handler (no checks for window.event required). It normalizes the target, relatedTarget, which, metaKey and pageX/Y properties and provides both stopPropagation() and preventDefault() methods.
Those properties are all documented, and accompanied by examples, on the Event object page.
The standard events in the Document Object Model are: blur, focus, load, resize, scroll, unload, beforeunload, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, and keyup. Since the DOM event names have predefined meanings for some elements, using them for other purposes is not recommended. jQuery's event model can trigger an event by any name on an element, and it is propagated up the DOM tree to which that element belongs, if any.
Element
An element in the Document Object Model (DOM) can have attributes, text, and children. It provides methods to traverse the parent and children and to get access to its attributes. Due to inconsistencies in DOM API specifications and implementations, however, those methods can be a challenge to use. jQuery provides a "wrapper" around those elements to help interacting with the DOM. But sometimes you will be working directly with DOM elements, or see methods that (also) accept DOM elements as arguments.
Whenever you call jQuery's .each() method or one of its event methods on a jQuery collection, the context of the callback function — this — is set to a DOM element.
Some properties of DOM elements are quite consistent among browsers. Consider this example of a simple onblur validation:
$( "input[type='text']" ).on( "blur", function() {
if( !this.value ) {
alert( "Please enter some text!" );
}
});
You could replace this.value with $(this).val() to access the value of the text input via jQuery, but in that case you wouldn't gain anything.
Text
Text is a node of the Document Object Model (DOM) that represents the textual content of an element or an attribute. Consider the following code:
Hello world
If you retrieve the children of the paragraph of the example as follows:
var children = document.getElementById( "target" ).childNodes;
you obtain two children. The first one is the element representing the b tag. The second child is a text node containing the string " world".
jQuery
A jQuery object contains a collection of Document Object Model (DOM) elements that have been created from an HTML string or selected from a document. Since jQuery methods often use CSS selectors to match elements from a document, the set of elements in a jQuery object is often called a set of "matched elements" or "selected elements".
The jQuery object itself behaves much like an array; it has a length property and the elements in the object can be accessed by their numeric indices [0] to [length-1]. Note that a jQuery object is not actually a Javascript Array object, so it does not have all the methods of a true Array object such as join().
Most frequently, you will use the jQuery() function to create a jQuery object. jQuery() can also be accessed by its familiar single-character alias of $(), unless you have called jQuery.noConflict() to disable this option. Many jQuery methods return the jQuery object itself, so that method calls can be chained:
In API calls that return jQuery, the value returned will be the original jQuery object unless otherwise documented by that API. API methods such as .filter() or .not() modify their incoming set and thus return a new jQuery object.
$( "p" ).css( "color", "red" ).find( ".special" ).css( "color", "green" );
Whenever you use a "destructive" jQuery method that potentially changes the set of elements in the jQuery object, such as .filter() or .find(), that method actually returns a new jQuery object with the resulting elements. To return to the previous jQuery object, you use the .end() method.
A jQuery object may be empty, containing no DOM elements. You can create an empty jQuery object with $() (that is, passing no arguments at all). A jQuery object may also be empty if a selector doesn't select any elements, or if a chained method filters out all the elements. It is not an error; any further methods called on that jQuery object simply have no effect since they have no elements to act upon. So, in this example if there are no bad entries on the page then no elements will be colored red:
$( ".badEntry" ).css({ color: "red" });
XMLHttpRequest
Some of jQuery's Ajax functions return the native XMLHttpRequest (XHR) object, or pass it as an argument to success/error/complete handlers, so that you can do additional processing or monitoring on the request. Note that Ajax functions only return or pass an XHR object when an XHR object is actually used in the request. For example, JSONP requests and cross-domain GET requests use a script element rather than an XHR object.
Although the XHR object is a standard, there are variations in its behavior on different browsers. Refer to the WHATWG site and Mozilla Developer Network for more information:
WHATWG living standard
Mozilla Developer Network
jqXHR
As of jQuery 1.5, the $.ajax() method returns the jqXHR object, which is a superset of the XMLHTTPRequest object. For more information, see the jqXHR section of the $.ajax entry
Thenable
Any object that has a then method.
Deferred Object
As of jQuery 1.5, the Deferred object provides a way to register multiple callbacks into self-managed callback queues, invoke callback queues as appropriate, and relay the success or failure state of any synchronous or asynchronous function.
Promise Object
This object provides a subset of the methods of the Deferred object (then, done, fail, always, pipe, progress, state and promise) to prevent users from changing the state of the Deferred.
Callbacks Object
A multi-purpose object that provides a powerful way to manage callback lists. It supports adding, removing, firing, and disabling callbacks. The Callbacks object is created and returned by the $.Callbacks function and subsequently returned by most of that function's methods.
Document
A document object created by the browser's DOM parser, usually from a string representing HTML or XML.
XML Document
A document object created by the browser's XML DOM parser, usually from a string representing XML. XML documents have different semantics than HTML documents, but most of the traversing and manipulation methods provided by jQuery will work with them.
Assert
A reference to or instance of the object holding all of QUnit's assertions. See the API documentation for QUnit.assert for details.
----------
20210829
Callback with Arguments.
Executing callbacks with arguments can be tricky.
須注意 回呼函數傳入參數的方式有點特別.
Unlike many other programming languages, JavaScript enables you to freely pass functions around to be executed at a later time. A callback is a function that is passed as an argument to another function and is executed after its parent function has completed. Callbacks are special because they patiently wait to execute until their parent finishes. Meanwhile, the browser can be executing other functions or doing all sorts of other work.
錯誤的寫法:
這樣是 立即執行(myCallBack( param1, param2 ))後, 再把回傳值, 當作$.get()的第2個參數.
$.get( "myhtmlpage.html", myCallBack( param1, param2 ) ); // 錯誤的寫法等於 $.get("myhtmlpage.html", 參數2為回傳值);
The reason this fails is that the code executes myCallBack( param1, param2 ) immediately and then passes myCallBack()'s return value as the second parameter to $.get(). We actually want to pass the function myCallBack(), not myCallBack( param1, param2 )'s return value (which might or might not be a function). So, how to pass in myCallBack() and include its arguments?
To defer executing myCallBack() with its parameters, you can use an anonymous function as a wrapper. Note the use of function() {. The anonymous function does exactly one thing: calls myCallBack(), with the values of param1 and param2.
When $.get() finishes getting the page myhtmlpage.html, it executes the anonymous function, which executes myCallBack( param1, param2 ).
正確的寫法:
以匿名函數 function(){...}, 當作$.get()的第2個參數傳入後, 再執行 myCallBack( param1, param2 );
$.get( "myhtmlpage.html", function() {
myCallBack( param1, param2 );
});
----------
20210829
document.ready()
$( document ).ready(function() { // 改寫在 jQuery.ready event 中, 則會在(網頁本身)載入後, 立即啟動程式執行.
// Your code here.
});
Launching Code on Document Ready
To ensure that their code runs after the browser finishes loading the document, many JavaScript programmers wrap their code in an onload function:
window.onload = function() { // JavaScript 通常在網頁完全(包括圖檔)載入後 onload event 中, 撰寫啟動程式執行.
alert( "welcome" );
};
Unfortunately, the code doesn't run until all images are finished downloading, including banner ads. To run code as soon as the document is ready to be manipulated, jQuery has a statement known as the ready event:
$( document ).ready(function() { // 改寫在 jQuery.ready event 中, 則會在(網頁本身)載入後, 立即啟動程式執行.
// Your code here.
});
For example, inside the ready event, you can add a click handler to the link:
$( document ).ready(function() {
$( "a" ).click(function( event ) { // 載入網頁後, 處理連結的函數.
alert( "Thanks for visiting!" );
});
});
Copy the above jQuery code into your HTML file where it says // Your code goes here. Then, save your HTML file and reload the test page in your browser. Clicking the link should now first display an alert pop-up, then continue with the default behavior of navigating to http://jquery.com.
For click and most other events, you can prevent the default behavior by calling event.preventDefault() in the event handler:
$( document ).ready(function() {
$( "a" ).click(function( event ) {
alert( "As you can see, the link no longer took you to jquery.com" );
event.preventDefault(); // click 預設會連接到屬性 href 的網址, 可以 event.preventDefault() 解除.
});
});
The following example illustrates the click handling code discussed above, embedded directly in the HTML . Note that in practice, it is usually better to place your code in a separate JS file and load it on the page with a 的方式存取.
完整範例如下:
Demo
jQuery