Home>

In addition to the built-in datatype in jQuery, jquery also includes some extended data types (virtual types), such as selectors, events, etc. Through this article, I will share 14 jquery data types.Interested friends study together

In addition to the built-in data types in jQuery, jquery also includes some extended data types (virtual types), such as selectors, events, and so on.

String

String is the most common and is supported in almost any high-level programming and scripting language.For example "hello world!" Is a string.The type of the string is string. such as

var typeofstr=typeof "hello world";//typeofstr is "string"

1.1 string built-in methods

"hello" .charat (0) //"h"

"hello" .touppercase () //"hello"

"hello" .tolowercase () //"hello"

"hello" .replace (/e | o/g, "x") //"hxllx"

"1,2,3" .split (",") //["1", "2", "3"]

1.2 length property:returns the length of the character,For example "hello" .length returns 5

1.3 String conversion to boolean:

An empty string ("") is false by default, and a non-empty string is true (such as "hello").

2. number

Number type,For example 3.1415926 or 1, 2, 3 ...

typeof 3.1415926 returns "number"

2.1 number is converted to boolean:

If a number value is 0, it defaults to false, otherwise it is true.

2.2 Since number is implemented using double-precision floating-point numbers,So the following situation is reasonable:

0.1 + 0.2 //0.30000000000000004

3. math

The following method is similar to the static method of the math class in Java.

math.pi //3.141592653589793

math.cos (math.pi) //-1

3.1 Stringification into numbers:parseint and parsefloat methods:

parseint ("123")=123 (with decimal conversion)

parseint ("010")=8 (with octal conversion)

parseint ("0xcafe")=51966 (with hexadecimal conversion)

parseint ("010", 10)=10 (specify decimal conversion)

parseint ("11", 2)=3 (specify binary conversion)

parsefloat ("10.10")=10.1

3.2 numbers to strings

When the number is appended to the (append) string,Will get the string.

"" + 1 + 2;//"12"

"" + (1 + 2);//"3"

"" + 0.0000001;//"1e-7"

Or use casts:

string (1) + string (2);//"12"

string (1 + 2);//"3"

4.nan and infinity

If you call the parseint method on a non-numeric string, nan (not a number) is returned.as follows:

isnan (parseint ("hello", 10)) //true

infinity means infinity or infinity,For example 1/0 //infinity.

Calling the typeof operator on both nan and infinity returns "numuber".

In addition nan == nan returns false, but infinity == infinity returns true.

5. integer and float

Divided into integer and floating point.

6. boolean

Boolean type,true or false.

7. object

Everything in JavaScript is an object.Performing a typeof on an object returns "object".

var x=();
var y={name:"pete", age:15};

For the above y object, you can use dots to get the attribute value,For example, y.name returns "pete" and y.age returns 15

7.1 array notation

var operations={increase:"++", decrease:"-"}
var operation="increase";
operations [operation] //"++";
operations ["multiply"]="*";//"*"

The above operations ["multiply"]="*";added a key-value pair to the operations object.

7.2 Object iteration:for-in

var obj={name:"pete", age:15};
for (key in obj) {
alert ("key is" + [key] + ", value is" + obj [key]);
}

7.3 Any object, regardless of its properties and values,Both default to true

7.4 Object's prototype property

Use fn (prototype alias) in jquery to dynamically add objects (functions) to jquery instances

var form=$("#myform");
form.clearform;//undefined
form.fn.clearform=function () {
return this.find (":input"). each (function () {this.value="";}). end ();
};
form.clearform () //works for all instances of jquery objects, because the new method was added

8. options

Almost all jquery plugins provide an options-based api. Options is a js object, which means that the object and its attributes are optional. Allow customization.

For example, submit the form using ajax.

$("#myform"). ajaxform ();//The default value of the form's action attribute is used as the ajax-url, and the method value is used as the post type (get/post)

$("#myform"). ajaxform ({url:"mypage.php", type:"post"});//Covers the submitted URL and submission type

9. array

var arr=[1, 2, 3];

array is a mutable list. Arrays are also objects.

Read or set the value of an element in an array,In this way:

var val=arr [0];//val is 1
arr [2]=4;//The third element of arr is now 4

9.1 Array looping (traversal)

for (var i=0;i<a.length;i ++) {//do something with a [i]}
But when considering performance,It is best to read the length property only once, as follows:
for (var i=0, j=a.length;i<j;i ++) {//do something with a [i]}
jquery provides each method to iterate through the array:
var x=[1, 2, 3];
$.each (x,function (index, value) {
console.log ("index", index, "value", value);
});

9.2 Calling the push method on an array means adding an element to the end of the array,For example x.push (5);and x. [X.length]=5;equivalent

9.3 Array other built-in methods:

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) //used to insert, delete or replace array elements
Here is to delete 2 elements starting from index=1

9.4 Arrays are objects,So always true

10. map

the map type is used by the ajax function to hold the data of a request. this type could be a string, an array<form elements>, a jquery object with form elements or an object with key/value pairs. in the last case , it is possible to assign multiple values ​​to one key by assigning an array. as below:
{"key []":["valuea", "valueb"]}

11. function:anonymous and famous

11.1 context, call, and apply

in javascript, the variable "this" always refers to the current context.
$(document) .ready (function () {
//this refers to window.document});
$("a"). click (function () {//this refers to an anchor dom element
});

12. selector

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.

13. event

dom standard events include:blur, focus, load, resize, scroll, unload, beforeunload, click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, andkeyup

14. jquery

The jquery object contains a collection of dom elements.For example $("p") returns all<p>...</p>

jquery objects behave like arrays,There is also a length property, and one of the dom element collections can also be accessed through the index.But not an array,Some methods that do not have arrays,For example join ().

Many jquery methods return the jquery object itself,So you can use chained calls:

$("p"). css ("color", "red"). find (". special"). css ("color", "green");

But if the method you call will destroy jquery objects, such as find () and filter (), the original object is not returned.To return to the original object, just call the end () method.

  • Previous Anti-aliasing solution for Android programming drawing
  • Next In-depth analysis of the usage and differences of sprintf and printf functions in PHP