Here we will discuss few jQuery techniques and Tricks for powerful developers which is useful for library. So starts with a few tips about performance & continue with little introductions to some of the jQuery library’s more features.
You should try to optimize the way you get elements not to force jQuery into traversing the whole DOM tree, which is too slow.
$('div[data-selected="true"] a') // Classy, but slow
$('div.selected a') // So so
$('#mydivId') // Good Every release of the jQuery library optimizes and fixes bugs, so most of the time upgrading involves only changing a script tag only.
Include jQuery directly from CDN servers, which provide free CDN hosting for a number of libraries.
Google CDN:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
Microsoft CDN:
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
Somehow, you need to create an empty jQuery object to fill it in with the add() method later.
var container = $([]);
container.add(element);
// Selecting all the navigation anchors:
var anchor = $(‘#nav a’);
If performance is always first choice, then simply use a for (or a while) loop instead of $.each(), that make your code several times faster. And checking the length is also the only way to check whether your data contains any elements.
if(buttons.length){
for(var i=0;i<anchor.length;i++){
....
}
} With the help of jQuery data() method, HTML5 data attributes are pulled and are available into scripts:
<div id="div_one" data-title="Div title" data-last-score="40" data-point="1" data-options='{"name":"Tim"}'>
...
</div> $("#d1").data("title"); // "Div title"
$("#d1").data("lastScore"); // 40
$("#d1").data("point"); // 1;
$("#d1").data("options").name; // "Tim" Creating a jQuery plugin is easier now:
(function($){
$.fn.your_plugin = function(){
// Your code goes here
return this;
};
})(jQuery); Pain in muscles and joints can be a problem in everyday life, whether it involves…
Most modern businesses spend a lot of time thinking about digital touchpoints. Websites, email campaigns,…
While TikTok and Instagram battle for users’ attention with short vertical videos, Elon Musk’s social…
When you are looking to apply for a mortgage the one thing that most people…
Once you've memorized basic blackjack strategy, it's time to delve deeper into the game. Advanced…
Yoga is no longer just a physical practice. Yoga is popular as a lifestyle practice.…