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
Copy file name to clipboardExpand all lines: page/using-jquery-core/attributes.md
+7-8
Original file line number
Diff line number
Diff line change
@@ -2,27 +2,26 @@
2
2
title : Attributes
3
3
level : beginner
4
4
---
5
+
5
6
An element's attributes can contain useful information for your application, so it's important to be able to get and set them.
6
7
7
-
## `.attr()`
8
+
## The `.attr()` method
8
9
9
10
The `.attr()` method acts as both a getter and a setter. As a setter, `.attr()` can accept either a key and a value, or an object containing one or more key/value pairs.
Copy file name to clipboardExpand all lines: page/using-jquery-core/avoid-conflicts-other-libraries.md
+67-95
Original file line number
Diff line number
Diff line change
@@ -7,164 +7,136 @@ attribution:
7
7
- jQuery Fundamentals
8
8
---
9
9
10
-
The jQuery library and virtually all of its plugins are contained within the
11
-
`jQuery` namespace. As a general rule, global objects are stored inside the
12
-
jQuery namespace as well, so you shouldn't get a clash between jQuery and any
13
-
other library (like prototype.js, MooTools, or YUI).
10
+
The jQuery library and virtually all of its plugins are contained within the `jQuery` namespace. As a general rule, global objects are stored inside the jQuery namespace as well, so you shouldn't get a clash between jQuery and any other library (like prototype.js, MooTools, or YUI).
14
11
15
-
That said, there is one caveat: *by default, jQuery uses `$` as a shortcut for
16
-
`jQuery`.* Thus, if you are using another JavaScript library that uses the `$`
17
-
variable, you can run into conflicts with jQuery. In order to avoid these
18
-
conflicts, you need to put jQuery in no-conflict mode immediately after it is
19
-
loaded onto the page and before you attempt to use jQuery in your page.
12
+
That said, there is one caveat: *by default, jQuery uses `$` as a shortcut for `jQuery`.* Thus, if you are using another JavaScript library that uses the `$` variable, you can run into conflicts with jQuery. 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.
20
13
21
-
##Putting jQuery into No-Conflict Mode
14
+
##Putting jQuery Into No-Conflict Mode
22
15
23
-
When you put jQuery into no-conflict mode, you have the option of assigning a
24
-
new variable name to replace the `$` alias.
16
+
When you put jQuery into no-conflict mode, you have the option of assigning a new variable name to replace the `$` alias.
25
17
26
18
```
27
-
<!-- Putting jQuery into no-conflict mode -->
19
+
<!-- Putting jQuery into no-conflict mode. -->
28
20
<script src="prototype.js"></script>
29
21
<script src="jquery.js"></script>
30
22
<script>
31
-
// $j is now an alias to the jQuery function;
32
-
// creating the new alias is optional
33
-
var $j = jQuery.noConflict();
34
-
$j(document).ready(function(){
35
-
$j("div").hide();
36
-
});
37
-
38
-
// The $ variable now has the prototype meaning,
39
-
// which is a shortcut for document.getElementById.
40
-
// mainDiv below is a DOM element, not a jQuery object
41
-
window.onload = function(){
42
-
var mainDiv = $('main');
43
-
}
23
+
24
+
var $j = jQuery.noConflict();
25
+
// $j is now an alias to the jQuery function; creating the new alias is optional.
26
+
27
+
$j(document).ready(function() {
28
+
$j( "div" ).hide();
29
+
});
30
+
31
+
// The $ variable now has the prototype meaning, which is a shortcut for
32
+
// document.getElementById(). mainDiv below is a DOM element, not a jQuery object.
33
+
window.onload = function() {
34
+
var mainDiv = $( "main" );
35
+
}
36
+
44
37
</script>
45
38
```
46
39
47
-
In the code above, the `$` will revert back to its meaning in original library.
48
-
You'll still be able to use the full function name `jQuery` as well as the new
49
-
alias `$j` in the rest of your application. The new alias can be named anything
50
-
you'd like: `jq`, `$J`, `awesomeQuery`, etc.
40
+
In the code above, the `$` will revert back to its meaning in original library. You'll still be able to use the full function name `jQuery` as well as the new alias `$j` in the rest of your application. The new alias can be named anything you'd like: `jq`, `$J`, `awesomeQuery`, etc.
51
41
52
-
Finally, if you don't want to define another alternative to the full `jQuery`
53
-
function name (you really like to use `$` and don't care about using the other
54
-
library's `$` method), then there's still another approach you might try:
55
-
simply add the `$` as an argument passed to your `jQuery(document).ready()`
56
-
function. This is most frequently used in the case where you still want the
57
-
benefits of really concise jQuery code, but don't want to cause conflicts with
58
-
other libraries.
42
+
Finally, if you don't want to define another alternative to the full `jQuery` function name (you really like to use `$` and don't care about using the other library's `$` method), then there's still another approach you might try: simply add the `$` as an argument passed to your `jQuery( document ).ready()` function. This is most frequently used in the case where you still want the benefits of really concise jQuery code, but don't want to cause conflicts with other libraries.
59
43
60
44
```
61
-
<!-- Another way to put jQuery into no-conflict mode -->
45
+
<!-- Another way to put jQuery into no-conflict mode. -->
62
46
<script src="prototype.js"></script>
63
47
<script src="jquery.js"></script>
64
48
<script>
65
49
66
-
jQuery.noConflict();
67
-
jQuery(document).ready(function($){
68
-
// You can use the locally-scoped $ in here as an alias to jQuery
69
-
$("div").hide();
70
-
});
50
+
jQuery.noConflict();
71
51
72
-
// The $ variable in the global scope has the prototype.js meaning
73
-
window.onload = function(){
74
-
var mainDiv = $('main');
75
-
}
52
+
jQuery( document ).ready(function( $ ) {
53
+
// You can use the locally-scoped $ in here as an alias to jQuery.
54
+
$( "div" ).hide();
55
+
});
76
56
77
-
</script>
57
+
// The $ variable in the global scope has the prototype.js meaning.
58
+
window.onload = function(){
59
+
var mainDiv = $( "main" );
60
+
}
78
61
62
+
</script>
79
63
```
80
64
81
-
This is probably the ideal solution for most of your code, considering that
82
-
there'll be less code that you'll have to change in order to achieve complete
83
-
compatibility.
65
+
This is probably the ideal solution for most of your code, considering that there'll be less code that you'll have to change in order to achieve complete compatibility.
84
66
85
-
##Including jQuery Before Other Libraries
67
+
##Including jQuery Before Other Libraries
86
68
87
-
The code snippets above rely on jQuery being loaded after prototype.js is
88
-
loaded. If you include jQuery before other libraries, you may use `jQuery` when
89
-
you do some work with jQuery, but the `$` will have the meaning defined in the
90
-
other library. There is no need to relinquish the `$` alias by calling
91
-
`jQuery.noConflict()`.
69
+
The code snippets above rely on jQuery being loaded after prototype.js is loaded. If you include jQuery before other libraries, you may use `jQuery` when you do some work with jQuery, but the `$` will have the meaning defined in the other library. There is no need to relinquish the `$` alias by calling `jQuery.noConflict()`.
92
70
93
71
```
94
-
<!-- Loading jQuery before other libraries -->
72
+
<!-- Loading jQuery before other libraries. -->
95
73
<script src="jquery.js"></script>
96
74
<script src="prototype.js"></script>
97
75
<script>
98
76
99
-
// Use full jQuery function name to reference jQuery
100
-
jQuery(document).ready(function(){
101
-
jQuery("div").hide();
102
-
});
77
+
// Use full jQuery function name to reference jQuery.
78
+
jQuery(document).ready(function(){
79
+
jQuery("div").hide();
80
+
});
103
81
104
-
// Use the $ variable as defined in prototype.js
105
-
window.onload = function() {
106
-
var mainDiv = $('main');
107
-
};
82
+
// Use the $ variable as defined in prototype.js
83
+
window.onload = function() {
84
+
var mainDiv = $( "main" );
85
+
};
108
86
109
87
</script>
110
88
```
111
89
112
-
##Summary of Ways to Reference the jQuery Function
90
+
##Summary of Ways to Reference the jQuery Function
113
91
114
-
Here's a recap of ways you can reference the jQuery function when the presence
115
-
of another library creates a conflict over the use of the `$` variable:
92
+
Here's a recap of ways you can reference the jQuery function when the presence of another library creates a conflict over the use of the `$` variable:
116
93
117
-
###Create a New Alias
94
+
###Create a New Alias
118
95
119
-
The `jQuery.noConflict()` method returns a reference to the jQuery function, so
120
-
you can capture it in whatever variable you'd like:
96
+
The `jQuery.noConflict()` method returns a reference to the jQuery function, so you can capture it in whatever variable you'd like:
121
97
122
98
```
123
99
<script src="prototype.js"></script>
124
100
<script src="jquery.js"></script>
125
101
<script>
126
102
127
-
// Give $ back to prototype.js; create new alias to jQuery
128
-
var $jq = jQuery.noConflict();
103
+
// Give $ back to prototype.js; create new alias to jQuery.
104
+
var $jq = jQuery.noConflict();
129
105
130
106
</script>
131
107
```
132
108
133
-
###Use an Immediately Invoked Function Expression
109
+
###Use an Immediately Invoked Function Expression
134
110
135
-
You can continue to use the standard `$` by wrapping your code in an
136
-
immediately invoked function expression; this is also a standard pattern for
137
-
jQuery plugin authoring, where the author cannot know whether another library
138
-
will have taken over the `$`. See the [Plugins](/plugins) section for more
139
-
information about writing plugins.
111
+
You can continue to use the standard `$` by wrapping your code in an immediately invoked function expression; this is also a standard pattern for jQuery plugin authoring, where the author cannot know whether another library will have taken over the `$`. See the [Plugins](/plugins/) section for more information about writing plugins.
140
112
141
113
```
142
-
<!-- Using the $ inside an immediately-invoked function expression -->
114
+
<!-- Using the $ inside an immediately-invoked function expression. -->
143
115
<script src="prototype.js"></script>
144
116
<script src="jquery.js"></script>
145
117
<script>
146
-
jQuery.noConflict();
147
118
148
-
(function($) {
149
-
// your jQuery code here, using the $
150
-
})(jQuery);
119
+
jQuery.noConflict();
120
+
121
+
(function( $ ) {
122
+
// Your jQuery code here, using the $
123
+
})( jQuery );
124
+
151
125
</script>
152
126
```
153
127
154
-
Note that if you use this technique, you will not be able to use prototype.js
155
-
methods inside the immediately invoked function that expect `$` to be
156
-
prototype.js's `$`.
128
+
Note that if you use this technique, you will not be able to use prototype.js methods inside the immediately invoked function that expect `$` to be prototype.js's `$`.
157
129
158
-
###Use the Argument That's Passed to the `jQuery(document).ready()` Function
130
+
###Use the Argument That's Passed to the `jQuery(document).ready()` Function
159
131
160
132
```
161
133
<script src="jquery.js"></script>
162
134
<script src="prototype.js"></script>
163
135
<script>
164
136
165
-
jQuery(document).ready(function($){
166
-
// your jQuery code here, using $ to refer to jQuery
167
-
});
137
+
jQuery(document).ready(function( $ ) {
138
+
// Your jQuery code here, using $ to refer to jQuery.
139
+
});
168
140
169
141
</script>
170
142
```
@@ -176,9 +148,9 @@ Or using the more concise syntax for the DOM ready function:
Note the style of the argument on the second line — it is an object that contains multiple properties. This is a common way to pass multiple arguments to a function, and many jQuery setter methods accept objects to set multiple values at once.
28
+
Note the style of the argument on the second line – it is an object that contains multiple properties. This is a common way to pass multiple arguments to a function, and many jQuery setter methods accept objects to set multiple values at once.
25
29
26
-
CSS properties that normally include a hyphen need to be camelCased in JavaScript. For example, the CSS property `font-size` is expressed as `fontSize` when used as a property name in JavaScript. However, this does not apply when passing the name of a CSS property to the `.css()` method as a string — in that case, either the camelCased or hyphenated form will work.
30
+
CSS properties that normally include a hyphen need to be camelCased in JavaScript. For example, the CSS property `font-size` is expressed as `fontSize` when used as a property name in JavaScript. However, this does not apply when passing the name of a CSS property to the `.css()` method as a string – in that case, either the camelCased or hyphenated form will work.
27
31
28
32
It's not recommended to use `.css()` as a setter in production-ready code, but when passing in an object to set CSS, CSS properties will be camelCased instead of using a hyphen.
29
33
@@ -32,14 +36,17 @@ It's not recommended to use `.css()` as a setter in production-ready code, but w
32
36
As a getter, the `.css()` method is valuable. However, it should generally be avoided as a setter in production-ready code, because it's generally best to keep presentational information out of JavaScript code. Instead, write CSS rules for classes that describe the various visual states, and then change the class on the element.
33
37
34
38
```
35
-
// Working with classes
36
-
var $h1 = $("h1");
39
+
// Working with classes.
40
+
41
+
var $h1 = $( "h1" );
37
42
38
-
$h1.addClass("big");
39
-
$h1.removeClass("big");
40
-
$h1.toggleClass("big");
43
+
$h1.addClass("big");
44
+
$h1.removeClass("big");
45
+
$h1.toggleClass("big");
41
46
42
-
if ( $h1.hasClass("big") ) { ... }
47
+
if ( $h1.hasClass( "big" ) ) {
48
+
...
49
+
}
43
50
```
44
51
45
52
Classes can also be useful for storing state information about an element, such as indicating that an element is selected.
@@ -51,24 +58,22 @@ jQuery offers a variety of methods for obtaining and modifying dimension and pos
51
58
The code below shows a brief overview of the dimensions functionality in jQuery. For complete details about jQuery dimension methods, visit the [dimensions documentation on api.jquery.com](http://api.jquery.com/category/dimensions/).
52
59
53
60
```
54
-
// Basic dimensions methods
55
-
56
-
// sets the width of all H1 elements
57
-
$("h1").width("50px");
58
-
// gets the width of the first H1
59
-
$("h1").width();
61
+
// Basic dimensions methods.
60
62
63
+
// Sets the width of all <h1> elements.
64
+
$( "h1" ).width( "50px" );
61
65
62
-
// sets the height of all H1 elements
63
-
$("h1").height("50px");
66
+
// Gets the width of the first <h1> element.
67
+
$("h1" ).width();
64
68
65
-
// gets the height of the first H1
66
-
$("h1").height();
69
+
// Sets the height of all <h1> elements.
70
+
$("h1").height( "50px" );
67
71
72
+
// Gets the height of the first <h1> element.
73
+
$( "h1" ).height();
68
74
69
-
// returns an object containing position
70
-
// information for the first H1 relative to
71
-
// its "offset (positioned) parent"
72
-
$("h1").position();
73
75
76
+
// Returns an object containing position information for
77
+
// the first <h1> relative to its "offset (positioned) parent".
0 commit comments