26 practical tips under Jquery (jQuery tips, tricks & solutions)_jquery
WBOY
Release: 2016-05-16 18:33:29
Original
1448 people have browsed it
For example, there are functions such as disabling right-clicking, hiding search text box text, opening links in new windows, detecting browsers, preloading images, switching page styles, equal heights of all columns, dynamically controlling page font size, and obtaining the X value and Y value of the mouse pointer. , verify whether the element is empty, replace the element, lazy load, verify whether the element exists in the Jquery collection, make the DIV clickable, clone the object, center the element, count the number of elements, use the Jquery class library on the Google host, disable Jquery The effect is to solve the conflict problem between Jquery class library and other Javascript class libraries.
$(document).ready(function () { //Example 1: Every link will open in a new window $('a[href^="http://"]').attr("target", "_blank") ; //Example 2: Links with the rel="external" attribute will only open in a new window $('a[@rel$='external']').click(function(){ this.target = "_blank"; }); }); // how to use open link
4 Detect browser Note: In version jQuery 1.4, $.support replaced the $.browser variable.
$(document).ready( function() { $("a.Styleswitcher").click(function() { //swicth the LINK REL attribute with the value in A REL attribute $('link[rel=stylesheet ]').attr('href' , $(this).attr('rel')); }); // how to use // place this in your header
$(document).ready(function() { // Reset the font size(back to default) var originalFontSize = $('html').css('font-size '); $(".resetFont").click(function(){ $('html').css('font-size', originalFontSize); }); // Increase the font size(bigger font0 $(".increaseFont").click(function(){ var currentFontSize = $('html').css('font-size'); var currentFontSizeNum = parseFloat(currentFontSize, 10); var newFontSize = currentFontSizeNum*1.2; $('html').css('font-size', newFontSize); return false; }); // Decrease the font size(smaller font) $(".decreaseFont").click(function(){ var currentFontSize = $('html').css(' font-size'); var currentFontSizeNum = parseFloat(currentFontSize, 10); var newFontSize = currentFontSizeNum*0.8; $('html').css('font-size', newFontSize); return false; }); });
$(document).ready(function() { $('a[href*=#]').click(function () { if (location.pathname.replace(/^//,'') == this.pathname.replace(/^//,'') && location.hostname == this.hostname ) { var $target = $(this.hash); $target = $target.length && $target || $('[name=' this.hash.slice(1) ' ]'); if ($target.length) { var targetOffset = $target.offset().top; $('html,body') .animate({scrollTop: targetOffset}, 900); return false; } } }); // how to use // place this where you want to scroll to // the link go to top });
$ (document).ready(function() { $().mousemove(function(e){ //display the x and y axis values inside the div with the id XY $('# XY').html("X Axis : " e.pageX " | Y Axis " e.pageY); }); // how to use
$( document).ready(function() { $("div").click(function(){ //get the url from href attribute and launch the url window.location=$(this ).find("a").attr("href"); return false; }); // how to use
$(document).ready(function() { $.extend($.expr[':'], { moreThen1000px: function(a) { return $(a).width() > 1000; } }); $('.box:moreThen1000px').click(function() { // creating a simple js alert box alert('The element that you have clicked is over 1000 pixels wide'); }); });
> Then use jQuery to hide the Loading DIV after all images are loaded. > Don’t forget to load jQuery library. When I was testing the code just now, I wrote the wrong address and almost went crazy.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn