Seven useful jQuery code snippets

Source: Internet
Author: User

Seven useful jQuery code snippets

This article mainly introduces seven useful jQuery tips, this article provides practical code snippets such as opening links in a new window, setting high columns, jQuery pre-loading images, right-clicking disabled, and timer setting. For more information, see

JQuery is a lightweight JavaScript library and one of the most popular Client HTML scripts. It is very famous among WEB designers and developers, there are also many useful plug-ins and technologies to help WEB developers develop creative and beautiful WEB pages.

Today, we will share some tips for jQuery users. These tips will help you to demonstrate the creativity and functionality of your website layout and applications.

1. Open the link in a new window

With the following code, you can click the link to open it in a new window:

?

1

2

3

4

5

$ (Document). ready (function (){

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

// And apply the target = _ blank

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

});

2. Set high-level Columns

The code below can make every column of height of your WEB application think about it:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<Div class = "Adjust height" style = "border: 1px solid">

<P> First Line </p>

<P> Second Line </p>

<P> Third Line </p>

</Div>

<Div class = "Adjust height" style = "border: 1px solid">

<P> Column Two </p>

</Div>

<Script src = "http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"> </script>

<Script>

$ (Document). ready (function (){

Watermark height ('. Watermark height ');

});

// Global variable, this will store the highest height value

Var maxHeight = 0;

Function compute height (col ){

// Get all the element with class = col

Col = $ (col );

// Loop all the col

Col. each (function (){

// Store the highest value

If ($ (this). height ()> maxHeight ){

MaxHeight = $ (this). height ();

}

});

// Set the height

Col. height (maxHeight );

}

</Script>

Iii. jQuery pre-Loaded Images

This tip can speed up page image loading:

?

1

2

3

4

5

6

7

8

9

10

11

JQuery. preloadImagesInWebPage = function (){

For (var ctr = 0; ctr & lt; arguments. length; ctr ++ ){

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

}

}

// Usage:

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

// Check whether the image is loaded

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

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

});

4. Right-click Disabled

?

1

2

3

4

5

6

7

8

9

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

});

});

5. Set the timer

?

1

2

3

4

5

$ (Document). ready (function (){

Window. setTimeout (function (){

// Some code

},500 );

});

6. Calculate the number of child elements

?

1

2

3

4

5

6

7

8

9

10

11

12

<Div id = "foo">

<Div id = "bar"> </div>

<Div id = "baz">

<Div id = "biz"> </div>

<Span> </span>

</Div>

</Div>

<Script src = "http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"> </script>

<Script type = "text/javascript">

// JQuery code to count child elements $ ("# foo> div"). size ()

Alert ($ ("# foo> div"). size ())

</Script>

7. Locate the element in the middle of the page

?

1

2

3

4

5

6

7

8

9

10

11

12

<Div id = "foo" style = "width: 200px; height: 200px; background: # ccc;"> </div>

<Script src = "http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"> </script>

<Script type = "text/javascript">

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;

}

// Use the above function:

$ ('# Foo'). center ();

</Script>

Related Article

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.