Using jQuery property “jQuery.fx.off”, which when set to true, disables all the jQuery animation. When this is done, all animation methods will immediately set elements to their final state when called, rather than displaying an effect.
The .animate() method allows us to create animation effects on any numeric CSS property. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect.
- styles: Specifies one or more CSS properties/values toanimate.
- duration: Optional. Specifies the speed of the animation.
- easing: Optional. Specifies the speed of the element in different points of the animation. Default value is “swing”.
- callback: Optional. A function to be executed after the animation completes.
Simple use of animate function is,
Hide Copy Code
eq() returns the element as a jQuery object. This method constructs a new jQuery object from one element within that set and returns it. That means that you can use jQuery functions on it.
get() return a DOM element. The method retrieve the DOM elements matched by the jQuery object. But as it is a DOM element and it is not a jQuery-wrapped object. So jQuery functions can’t be used.
The basic difference is the parent() function travels only one level in the DOM tree, where parents() function search through the whole DOM tree.
$(‘<div/>’) : This creates a new div element. However this is not added to DOM tree unless you don’t append it to any DOM element.
$(‘div’) : This selects all the div element present on the page.
.size() method returns number of element in the object. But it is not preferred to use the
size()method as jQuery provide
.length property and which does the same thing. But the
.length property is preferred because it does not have the overhead of a function call.
The $.each() function is used to iterate over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array.
this and $(this) refers to the same element. The only difference is the way they are used. ‘this’ is used in traditional sense, when ‘this’ is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.
To work with an element on the web page, first we need to find them. To find the html element in jQuery we use selectors. There are many types of selectors but basic selectors are:
- Name: Selects all elements which match with the given element Name.
- #ID: Selects a single element which matches with the given ID
- .Class: Selects all elements which match with the given Class.
- Universal (*): Selects all elements available in a DOM.
- Attribute Selector: Select elements based on its attribute value.
It is a good approach to always use CDN but sometimes what if the CDN is down (rare possibility though) but you never know in this world as anything can happen.
Below given jQuery code checks whether jQuery is loaded from Google CDN or not, if not then it references the jQuery.js file from your folder.
if (typeof jQuery == ‘undefined’)
It first loads the jQuery from Google CDN and then check the jQuery object. If jQuery is not loaded successfully then it will references the jQuery.js file from hard drive location. In this example, the jQuery.js is loaded from Scripts folder.