You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you are using another JavaScript library that uses the `$` variable, you can run into conflicts with jQuery.
10
-
In order to avoid these conflicts, you need to put jQuery in no-conflict mode immediately after it is loaded onto the page and before you attempt to use jQuery in your page.
9
+
If you are using another JavaScript library that uses the `$` variable, you can
10
+
run into conflicts with jQuery. In order to avoid these conflicts, you need to
11
+
put jQuery in no-conflict mode immediately after it is loaded onto the page and
12
+
before you attempt to use jQuery in your page.
11
13
12
-
When you put jQuery into no-conflict mode, you have the option of assigning a variable name to replace `$`.
14
+
When you put jQuery into no-conflict mode, you have the option of assigning a
15
+
variable name to replace `$`.
13
16
14
17
<divclass="example"markdown="1">
15
18
Putting jQuery into no-conflict mode
@@ -19,8 +22,10 @@ Putting jQuery into no-conflict mode
Until now, we’ve been dealing entirely with methods that are called on a jQuery object. For example:
9
+
Until now, we’ve been dealing entirely with methods that are called on a jQuery
10
+
object. For example:
10
11
11
12
<divclass="example"markdown="1">
12
13
$('h1').remove();
13
14
</div>
14
15
15
-
Most jQuery methods are called on jQuery objects as shown above;
16
-
these methods are said to be part of the `$.fn` namespace, or the “jQuery prototype,” and are best thought of as jQuery object methods.
16
+
Most jQuery methods are called on jQuery objects as shown above; these methods
17
+
are said to be part of the `$.fn` namespace, or the “jQuery prototype,” and are
18
+
best thought of as jQuery object methods.
17
19
18
-
However, there are several methods that do not act on a selection;
19
-
these methods are said to be part of the jQuery namespace, and are best thought of as core jQuery methods.
20
+
However, there are several methods that do not act on a selection; these
21
+
methods are said to be part of the jQuery namespace, and are best thought of as
22
+
core jQuery methods.
20
23
21
-
This distinction can be incredibly confusing to new jQuery users. Here’s what you need to remember:
24
+
This distinction can be incredibly confusing to new jQuery users. Here’s what
25
+
you need to remember:
22
26
23
-
* Methods called on jQuery selections are in the `$.fn` namespace, and automatically receive and return the selection as this.
24
-
* Methods in the $ namespace are generally utility-type methods, and do not work with selections; they are not automatically passed any arguments, and their return value will vary.
27
+
* Methods called on jQuery selections are in the `$.fn` namespace, and
28
+
automatically receive and return the selection as this.
29
+
* Methods in the $ namespace are generally utility-type methods, and do not
30
+
work with selections; they are not automatically passed any arguments, and
31
+
their return value will vary.
25
32
26
-
There are a few cases where object methods and core methods have the same names, such as `$.each` and `$.fn.each`. In these cases, be extremely careful when reading the documentation that you are exploring the correct method.
33
+
There are a few cases where object methods and core methods have the same
34
+
names, such as `$.each` and `$.fn.each`. In these cases, be extremely careful
35
+
when reading the documentation that you are exploring the correct method.
Although jQuery eliminates most JavaScript browser quirks, there are still occasions when your code needs to know about the browser environment.
9
+
Although jQuery eliminates most JavaScript browser quirks, there are still
10
+
occasions when your code needs to know about the browser environment.
10
11
11
-
jQuery offers the `$.support` object, as well as the deprecated $.browser object, for this purpose.
12
-
For complete documentation on these objects, visit [http://api.jquery.com/jQuery.support/](http://api.jquery.com/jQuery.support/"$.support documentation on api.jquery.com") and [http://api.jquery.com/jQuery.browser/](http://api.jquery.com/jQuery.browser/"$.browser support on api.jquery.com").
12
+
jQuery offers the `$.support` object, as well as the deprecated $.browser
13
+
object, for this purpose. For complete documentation on these objects, visit
The `$.support` object is dedicated to determining what features a browser supports; it is recommended as a more “future-proof” method of customizing your JavaScript for different browser environments.
19
+
The `$.support` object is dedicated to determining what features a browser
20
+
supports; it is recommended as a more “future-proof” method of customizing your
21
+
JavaScript for different browser environments.
15
22
16
-
The `$.browser` object was deprecated in favor of the `$.support` object, but it will not be removed from jQuery anytime soon. It provides direct detection of the browser brand and version.
23
+
The `$.browser` object was deprecated in favor of the `$.support` object, but
24
+
it will not be removed from jQuery anytime soon. It provides direct detection
Copy file name to clipboardExpand all lines: content/jquery-fundamentals/using-jquery-core/utility-methods.md
+21-14Lines changed: 21 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,12 @@ attribution: jQuery Fundamentals
6
6
---
7
7
## Utility Methods
8
8
9
-
jQuery offers several utility methods in the $ namespace.
10
-
These methods are helpful for accomplishing routine programming tasks.
11
-
Below are examples of a few of the utility methods; for a complete reference on jQuery utility methods, visit [http://api.jquery.com/category/utilities/](http://api.jquery.com/category/utilities/"Utilities documenations on api.jquery.com").
9
+
jQuery offers several utility methods in the $ namespace. These methods are
10
+
helpful for accomplishing routine programming tasks. Below are examples of a
11
+
few of the utility methods; for a complete reference on jQuery utility methods,
@@ -36,7 +39,8 @@ Iterates over arrays and objects.
36
39
<divclass="note"markdown="1">
37
40
### Note
38
41
39
-
There is also a method `$.fn.each`, which is used for iterating over a selection of elements.
42
+
There is also a method `$.fn.each`, which is used for iterating over a
43
+
selection of elements.
40
44
</div>
41
45
42
46
### $.inArray
@@ -45,7 +49,7 @@ There is also a method `$.fn.each`, which is used for iterating over a selection
45
49
Returns a value's index in an array, or -1 if the value is not in the array.
46
50
47
51
var myArray = [ 1, 2, 3, 5 ];
48
-
52
+
49
53
if ($.inArray(4, myArray) !== -1) {
50
54
console.log('found it!');
51
55
}
@@ -58,18 +62,19 @@ Changes the properties of the first object using the properties of subsequent ob
58
62
59
63
var firstObject = { foo : 'bar', a : 'b' };
60
64
var secondObject = { foo : 'baz' };
61
-
65
+
62
66
var newObject = $.extend(firstObject, secondObject);
63
67
console.log(firstObject.foo); // 'baz'
64
68
console.log(newObject.foo); // 'baz'
65
69
</div>
66
70
67
71
<divclass="example"markdown="1">
68
-
If you don't want to change any of the objects you pass to `$.extend`, pass an empty object as the first argument.
72
+
If you don't want to change any of the objects you pass to `$.extend`, pass an
73
+
empty object as the first argument.
69
74
70
75
var firstObject = { foo : 'bar', a : 'b' };
71
76
var secondObject = { foo : 'baz' };
72
-
77
+
73
78
var newObject = $.extend({}, firstObject, secondObject);
74
79
console.log(firstObject.foo); // 'bar'
75
80
console.log(newObject.foo); // 'baz'
@@ -78,26 +83,28 @@ If you don't want to change any of the objects you pass to `$.extend`, pass an e
78
83
### $.proxy
79
84
80
85
<divclass="example"markdown="1">
81
-
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.
86
+
Returns a function that will always run in the provided scope — that is, sets
87
+
the meaning of this inside the passed function to the second argument.
82
88
83
89
var myFunction = function() { console.log(this); };
84
90
var myObject = { foo : 'bar' };
85
-
91
+
86
92
myFunction(); // logs window object
87
-
93
+
88
94
var myProxyFunction = $.proxy(myFunction, myObject);
89
95
myProxyFunction(); // logs myObject object
90
96
</div>
91
97
92
98
<divclass="example"markdown="1">
93
-
If you have an object with methods, you can pass the object and the name of a method to return a function that will always run in the scope of the object.
99
+
If you have an object with methods, you can pass the object and the name of a
100
+
method to return a function that will always run in the scope of the object.
94
101
95
102
var myObject = {
96
103
myFn : function() {
97
104
console.log(this);
98
105
}
99
106
};
100
-
107
+
101
108
$('#foo').click(myObject.myFn); // logs DOM element #foo
0 commit comments