


26 practical tips under Jquery (jQuery tips, tricks & solutions)_jquery
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.
The details are as follows:
1. Right-click is prohibited
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
2. Hide the search text box text
$(document).ready(function() {
$("input.text1").val("Enter your search text here");
textFill($ ('input.text1'));
});
function textFill(input){ //input focus text function
var originalvalue = input.val();
input.focus( function (){
if( $.trim(input.val()) == originalvalue ){ input.val(''); }
});
input.blur( function(){
if( $.trim(input.val()) == '' ){ input.val(originalvalue); }
});
}
3 in new Open the link in the window
$(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() {
// Target Firefox 2 and above
if ($.browser.mozilla && $.browser.version >= "1.8" ){
// do something
}
// Target Safari
if( $.browser.safari ){
// do something
}
// Target Chrome
if( $.browser.chrome){
// do something
}
// Target Camino
if( $.browser.camino){
// do something
}
// Target Opera
if( $.browser. opera){
// do something
}
// Target IE6 and below
if ($.browser.msie && $.browser.version <= 6 ){
// do something
}
// Target anything above IE6
if ($.browser.msie && $.browser.version > 6){
// do something
}
});
5 Preload images
$(document).ready(function() {
jQuery.preloadImages = function()
{
for(var i = 0; i").attr("src", arguments [i]);
}
};
// how to use
$.preloadImages("image1.jpg");
});
6 Page style switching
$(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
// the links
Default Theme A>
Red Theme
Blue Theme< ;/A>
});
7 columns have the same height
If two CSS columns are used, this method can be used to make the heights of the two columns the same.
$(document).ready(function() {
function equalHeight(group) {
tallest = 0;
group.each(function() {
thisHeight = $(this).height();
if(thisHeight > tallest) {
tallest = thisHeight;
}
});
group.height(tallest);
}
// how to use
$(document).ready( function() {
equalHeight($(".left"));
equalHeight($(".right"));
});
});
8 Dynamically control the page font size
Users can change the page font size
$(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;
});
});
9 Return to the top of the page function
$(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
});
10 Get the mouse pointerXY value
$ (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
});
11 Verify whether the element is empty
$(document).ready(function() {
if ($('#id').html()) {
// do something
}
});
12 Replacement elements
$(document).ready(function() {
$('#id').replaceWith('
');
});
13 jQuery delayed loading function
$(document).ready(function() {
window.setTimeout(function() {
// do something
}, 1000);
});
14 Remove word function
$(document).ready(function() {
var el = $('#id');
el.html(el.html().replace(/word /ig, ""));
});
15 Verify whether the element exists in the Jquery object collection
$(document).ready(function() {
if ($('#id').length) {
// do something
}
});
16 Make the entire DIV clickable
$( 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
});
17 Switch between ID and Class When changing the Window size, switch between ID and Class
$(document).ready(function() {
function checkWindowSize() {
if ( $(window).width() > 1200 ) {
$('body').addClass('large');
}
else {
$('body').removeClass('large');
}
}
$(window).resize(checkWindowSize);
});
18 Clone object
$(document).ready(function () {
var cloned = $('#id').clone();
// how to use
});
19 Position the element in the middle of the screen
$(document).ready(function() {
jQuery.fn.center = function () {
this.css("position","absolute");
this.css ("top", ( $(window).height() - this.height() ) / 2 $(window).scrollTop() "px");
this.css("left", ( $( window).width() - this.width() ) / 2 $(window).scrollLeft() "px");
return this;
}
$("#id").center ();
});
20 Write your own selector
$(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');
});
});
21 statistical elements Number
$(document).ready(function() {
$("p").size();
});
22 Use your own Bullets
$(document).ready(function() {
$("ul").addClass("Replaced") ;
$("ul > li").prepend("‒ ");
// how to use
ul.Replaced { list-style : none; }
});
23 Reference the Jquery class library on Google host Let Google host the jQuery script for you. This can be done in 2 ways.
//Example 1
// Example 2:(the best and fastest way)
24. Disable Jquery (animation) effect
$ (document).ready(function() {
jQuery.fx.off = true;
});
25. Solution to conflicts with other Javascript libraries
$(document).ready(function() {
var $ jq = jQuery.noConflict();
$jq('#id').show();
});
26. Application of Load() function (page load Enter the prompt):
> First write the CSS and position it absolutely to the upper right corner of the page.
#loading {
position:absolute; z- index:900;text-align:center;
background-color:#eef2fa;border:1px solid #d8e3e8;
color:#000;font-size:12px;padding:3px;width:80px;
right:0;top:0;
}
> 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.
<script> <br>jQuery(document). ready(function($){ <br>$('img').load(function(){ <br> $('#loading').css("display","none"); <br>}) ; <br>}); <br></script>
> Just insert a DIV anywhere, O(∩_∩)O haha.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
