javascript
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| javascript [2021/05/16 14:41] – admin | javascript [2021/07/23 14:52] (current) – admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | # DOM | + | # |
| + | |||
| + | ## DOM | ||
| The HTML DOM is a standard for how to get, change, add, or delete HTML elements. | The HTML DOM is a standard for how to get, change, add, or delete HTML elements. | ||
| Line 5: | Line 7: | ||
| https:// | https:// | ||
| - | # jQuery | + | ## Basic |
| + | |||
| + | ### Datatype | ||
| + | |||
| + | ``` | ||
| + | var length = 16; // Number | ||
| + | var lastName = " | ||
| + | var x = {firstName:" | ||
| + | ``` | ||
| + | |||
| + | ### Object | ||
| + | Create an object with 3 properties: | ||
| + | ```javascript | ||
| + | var object = { | ||
| + | ID: 1, | ||
| + | " | ||
| + | grade: { | ||
| + | math: 94 | ||
| + | physics: | ||
| + | } | ||
| + | // When a function is used as an object property, it is called a method | ||
| + | sum: function(a, | ||
| + | return a+b; | ||
| + | } | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | Use dot ```.``` or bracket ```[]``` to access or create properties | ||
| + | ```javascript | ||
| + | object.ID = object.ID + 1; | ||
| + | object[" | ||
| + | object.grade.math = 100; | ||
| + | // Method can be called in the same way | ||
| + | object.sum(1, | ||
| + | |||
| + | // Create new properties or methods in the same way | ||
| + | object[name + " | ||
| + | object.minus = function(a, | ||
| + | return a-b; | ||
| + | }; | ||
| + | ``` | ||
| + | |||
| + | ### Question Mark "?" | ||
| + | |||
| + | Three use of question mark: [[https:// | ||
| + | |||
| + | ## jQuery | ||
| Basic syntax is: ````$(selector).action()````\\ | Basic syntax is: ````$(selector).action()````\\ | ||
| Line 12: | Line 60: | ||
| A jQuery action() to be performed on the element(s)\\ | A jQuery action() to be performed on the element(s)\\ | ||
| + | By default, jQuery uses " | ||
| - | ## The $ mark | ||
| - | By default, jQuery uses " | ||
| - | Doing ````var $=function(){}```` is just defining | + | ### Start a jQuery codeblock |
| - | + | ```javascript | |
| - | ## Start a jQuery codeblock | + | |
| - | + | ||
| - | ``` | + | |
| $(function() { | $(function() { | ||
| - | | + | // jQuery methods go here... |
| }); | }); | ||
| ``` | ``` | ||
| is a short version of | is a short version of | ||
| - | ``` | + | ```javascript |
| $(document).ready(function() { | $(document).ready(function() { | ||
| - | | + | // jQuery methods go here... |
| }); | }); | ||
| ``` | ``` | ||
| - | ``` | + | ```javascript |
| - | (function (d) { | + | (function ( $ ) { |
| - | ... | + | // jQuery methods go here... |
| })(jQuery) | })(jQuery) | ||
| ``` | ``` | ||
| Line 42: | Line 86: | ||
| $(document).ready(function() { | $(document).ready(function() { | ||
| // Assign all list items on the page to be the color red. | // Assign all list items on the page to be the color red. | ||
| - | // This does not work until AFTER the entire DOM is " | + | // This does not work until AFTER the entire DOM is " |
| $(' | $(' | ||
| }); | }); | ||
| ``` | ``` | ||
| - | The pseudo-code for that block is: | + | The pseudo-code for that block is: When the document object model ````$(document) is ready .ready()````, |
| - | When the document object model ````$(document) is ready .ready()````, | ||
| + | ### Functions in jQuery | ||
| - | ## Functions in jQuery | + | Example: hide the current element |
| - | + | ||
| - | **Example:** hide the current element | + | |
| ```javascript | ```javascript | ||
| $(this).hide() | $(this).hide() | ||
| ``` | ``` | ||
| - | **Example:** select the element with id="# | + | Example: select the element with id="# |
| ```javascript | ```javascript | ||
| $("# | $("# | ||
| Line 66: | Line 108: | ||
| ``` | ``` | ||
| - | **Example:** define a new function " | + | Example: define a new function " |
| ```javascript | ```javascript | ||
| $.publish = function (topic, args) { | $.publish = function (topic, args) { | ||
| Line 73: | Line 115: | ||
| ``` | ``` | ||
| - | **Example:** use " | + | Example: use " |
| ```javascript | ```javascript | ||
| $(" | $(" | ||
| Line 88: | Line 130: | ||
| ``` | ``` | ||
| - | ### callback | + | #### jQuery plugins |
| - | For example, in ```$(selector).show(speed, | + | Example: to create a plugin that makes text within a set of retrieved elements green. All we have to do is add a function |
| + | |||
| + | ```javascript | ||
| + | $.fn.greenify = function() { | ||
| + | this.css( " | ||
| + | return this; // make your function chainable | ||
| + | } | ||
| + | $( " | ||
| + | ``` | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | #### Callback function | ||
| + | JavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. | ||
| + | |||
| + | To prevent this, you can create a callback function. A callback function is executed after the current effect is finished. Typical syntax: | ||
| + | |||
| + | Example: | ||
| + | |||
| + | ```javascript | ||
| + | $(" | ||
| + | $(" | ||
| + | alert(" | ||
| + | }); | ||
| + | }); | ||
| + | ``` | ||
| + | |||
| + | #### Chaining functions | ||
| + | The following example chains together the css(), slideUp(), and slideDown() methods. The " | ||
| + | |||
| + | ```javascript | ||
| + | $("# | ||
| + | ``` | ||
| + | |||
| + | #### Normal javascript function in jQuery? | ||
| + | You might want to just have a function | ||
| + | ```javascript | ||
| + | $(document).ready( function () { | ||
| + | var MyBlah = function(blah) { alert(blah); | ||
| + | MyBlah(" | ||
| + | }); | ||
| + | |||
| + | MyBlah(" | ||
| + | ``` | ||
| + | |||
| + | This is (sometimes) a desirable behavior since we do not pollute the global namespace, so if your function does not need to be called from other part of your code, this is the way to go. | ||
| + | |||
| + | Declaring it outside the anonymous function places it in the global namespace, and it's accessible from everywhere. | ||
| + | |||
| + | If you want to call it anywhere, use the `$.` namespace: | ||
| + | ```javascript | ||
| + | $(document).ready( function () { | ||
| + | $.MyBlah = function(blah) { alert(blah); | ||
| + | MyBlah(" | ||
| + | }); | ||
| + | |||
| + | $.MyBlah(" | ||
| + | ``` | ||
javascript.1621147288.txt.gz · Last modified: (external edit)
