10 jquery code Snippets for efficient web development _jquery

Source: Internet
Author: User

For the past few years, jquery has been the most widely used JavaScript script library. Today we will provide you with 10 of the most useful jquery snippets for web developers, and the developers who need them will be able to save them.

1, Detect Internet Explorer version
Internet Explorer has been a problem for developers and designers when it comes to CSS design. Although IE6 's dark Age has passed, ie has become less popular, it is always a good thing to detect easily. Of course, the following code can also be used to detect other browsers.

 $ (document). Ready (function () {
   if (Navigator.userAgent.match (/msie/i)) {
    alert (' I am ' an old fashioned Internet Explorer ');
   }
); 

2, smooth sliding to the top of the page
This is one of the most widely used jquery effects: Clicking on a link will smoothly move the page to the top. There's nothing new here, but every developer has to write a similar function occasionally.

 $ ("a[href= ' #top ']"). Click (function () {
 $ (HTML, body). Animate ({scrolltop:0}, "slow");
 return false;
}); 

3, fixed at the top of the
a very useful piece of code that allows an element to be fixed at the top. is super useful for navigation buttons, toolbars, or important information boxes.

 $ (function () {
 var $win = $ (window)
 var $nav = $ ('. Mytoolbar ');
 var navtop = $ ('. Mytoolbar '). Length && $ ('. Mytoolbar '). Offset (). Top;
 var isfixed=0;

 Processscroll ()
 $win. On (' scroll ', processscroll)

 function Processscroll () {
 var i, scrolltop = $ Win.scrolltop ()

 if (scrolltop >= navtop &&!isfixed) { 
 isfixed = 1
 $nav. addclass (' Subnav-fixed ')
 } else if (scrolltop <= navtop && isfixed) {
 isfixed = 0
  $nav. Removeclass (' Subnav-fixed ')
 }
} 

4. Replace HTML logo with other content
jquery makes it easy to replace HTML flags with something else. There is no room to be used.

 $ (' Li '). ReplaceWith (function () {return
 $ ("<div/>"). Append ($ (this). contents ());
}); 

5, detection window width
now that mobile devices are more common than outdated computers, it is helpful to be able to easily detect a smaller window width. Luckily, using jquery is super simple.

 var responsive_viewport = $ (window). width ();

/* If is below 481px
/if (Responsive_viewport < 481) {
  alert (' viewport is smaller than 481px. ');
}/* End Smallest screen * * 

6, automatically locate and repair damaged pictures
If your site is large and has been running online for many years, you will more or less encounter damaged images somewhere in the interface. This useful function can help detect the corrupted picture and replace it with your favorite picture, and will notify the visitor of the problem.

 $ (' img '). Error (function () {
 $ (this). attr (' src ', ' img/broken.png ');
}); 

7. Detect copy, paste and cut operations
using jquery makes it easy to detect copy, paste, and cut operations according to your requirements.

 $ ("#textA"). Bind (' Copy ', function () {
  $ (' span '). Text (' Copy Behaviour detected! ')
}); 
$ ("#textA"). Bind (' Paste ', function () {
  $ (' span '). Text (' Paste Behaviour detected! ')
}); 
$ ("#textA"). Bind (' Cut ', function () {
  $ (' span '). Text (' Cut Behaviour detected! ')
}); 

8, Encounter external links automatically add target= "blank" Properties
when linking to an external site, you might use the target= "blank" property to open the site in a new interface. The problem is that the target= "blank" attribute is not a valid property of the consortium. Let's use jquery to remedy: The following code will detect whether the link is outside the chain, and if so, automatically add a target= "blank" attribute.

 var root = Location.protocol + '//' + Location.host;
$ (' a '). Not (': Contains (root) '). Click (function () {
  this.target = "_blank";
}); 

9, in the picture to stay on the gradual increase or weaken the transparency effect
another "classic" code that you want to put into your toolbox because you are going to implement it from time to times.

 $ (document). Ready (function () {
  $ (". Thumbs img"). Fadeto ("slow", 0.6);//This sets the opacity of the thumbs to Fade Dow N to 60% when the page loads

  $ (". Thumbs img"). Hover (function () {
    $ (this). Fadeto ("Slow", 1.0);//This should set T 100%
  on Hover},function () {
    $ (a). Fadeto ("slow", 0.6);//This should set the opacity 60 % on Mouseout
  });
 

10. Prohibit the space bar when text or password is entered
There is no need for space keys in many forms, such as email, username, password, etc. Here is a simple technique that you can use to disable the SPACEBAR in the selected input.

 $ (' Input.nospace '). KeyDown (function (e) {
 if (E.keycode =) {return
 false;
 }
});

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.