8 practical jQuery skills

Source: Internet
Author: User

8 practical jQuery skills

This article mainly introduces eight practical jQuery tips, such as disabling right-click, returning to the top, and pre-loading images. For more information, see

1) disable the right-click Function

If you want to protect your website information, you can use this code to disable the right-click function.

 

The Code is as follows:

$ (Document). ready (function (){

// Catch the right-click context menu

$ (Document). bind ("contextmenu", function (e ){

// Warning prompt-optional

Alert ("No right-clicking! ");

 

// Delete the default context menu

Return false;

});

});

 

2) use jQuery to set the text size

With this code, you can reset the text size (increase or decrease) as needed ).

The Code is as follows:

$ (Document). ready (function (){

// Find the current font size

Var originalFontSize = ('html').css ('font-size ');

 

// Increase the text size

$ (". IncreaseFont"). click (function (){

Var currentFontSize = ('html').css ('font-size ');

Var currentFontSizeNumber = parseFloat (currentFontSize, 10 );

 

Var newFontSize = currentFontSizeNumber * 1.2;

(('Html').css ('font-size', newFontSize );

Return false;

});

 

// Decrease the Text Size

$ (". 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;

});

 

// Reset Font Size

$ (". ResetFont"). click (function (){

(('Html').css ('font-size', originalFontSize );

});

});

 

3) Open the link in a new window

Using this code will help you open a link in a new window to provide a better user experience.

The Code is as follows:

$ (Document). ready (function (){

// Select all anchor tags that have http in the href

// And apply the target = _ blank

$ ("A [href ^ = 'HTTP ']"). attr ('target',' _ blank ');

});

4) change the style list

Use this code to change the style list.

The Code is as follows: $ (document). ready (function (){

$ ("A.css Swap"). click (function (){

// Swap the link rel attribute with the value in the rel

$ ('Link [rel = stylesheet] '). attr ('href', $ (this). attr ('rel '));

});

});

5) return to the top Link

This code is very useful for clicking a single page for a long time. You can click "Back to Top" in an important moment.

The Code is as follows:

$ (Document). ready (function (){

// When the id = "top" link is clicked

$ ('# Top'). click (function (){

// Scoll the page back to the top

$ (Document). scrollTo (0,500 );

}

});

6) obtain the X/y axis of the mouse pointer

The Code is as follows: $ (). mousemove (function (e ){

// Display the x and y axis values inside the P element

('P'hangzhou.html ("X Axis:" + e. pageX + "| Y Axis" + e. pageY );

});

7) detect the current mouse coordinates

With this script, you can get the mouse coordinates in any web browser.

The Code is as follows: $ (document). ready (function (){

$ (). Mousemove (function (e)

{

$ ('# MouseCoordinates 'hangzhou.html ("X Axis Position =" + e. pageX + "and Y Axis Position =" + e. pageY );

});

8) image pre-loading

This Code helps you quickly load images or webpages.

The Code is as follows:

JQuery. preloadImagesInWebPage = function ()

{

For (var ctr = 0; ctr <arguments. length; ctr ++)

{

JQuery (""). attr ("src", arguments [ctr]);

}

}

To use the above method, you can use the following piece of code:

$. PreloadImages ("image1.gif", "image2.gif", "image3.gif ");

To check whether an image has been loaded, you can use the following piece of code:

$ ('# Imageobject'). attr ('src', 'image1.gif'). load (function (){

Alert ('image has been loaded... ');

});

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.