10 tips for using jquery in Visual Studio

Source: Internet
Author: User
Tags jquery library

The popular jquery is an open-source, cross-browser and JavaScript library compatible with CSS 3. You can use it to simplify your JavaScript coding tasks and operations (add, edit and delete) DOM elements in HTML content. This article describes 10 useful techniques for using jquery in Visual Studio.

What do you need to prepare?

To smoothly use jquery in Visual Studio, you need to install the following software:

Visual Studio 2008

Visual Studio 2008 SP1

Jquery Library

Visual Studio 2008 jquery plugin

You can also directly use Visual Studio 2010 because it has built-in support for jquery. If you happen to be using Visual Studio 2010, congratulations, The jquery library is installed by default.

Before using jquery to operate DOM elements on a webpage, let's take a look at jquery's introduction and its advantages.

Some useful jquery Techniques

The most notable features of jquery include:

Browser independence: jquery supports most browsers;

Simplified event processing model: jquery supports excellent, easy-to-use, and paradigm-based event processing models, greatly reducingCodeThe jquery event processing model is consistent across all browsers. The event object is a cross-browser standardized object, and the event object is always passed to the event processing as a parameter.Program;

Seamless extension: jquery provides extension support through easy-to-use plug-in APIs, allowing seamless extension of the jquery core library.

The following describes some tips for using jquery.

1. Use jquery to pre-load images

Pre-Loaded Images are considered a best practice because they increase the rendering speed of webpages. The following code shows the code snippet of jquery pre-Loaded Images:

    1. Jquery. preloadimages = function ()
    2. {
    3. For(VAR x = 0; x"). ATTR ("SRC ", arguments [x]);
    4. }};

2. Use jquery to disable context menus

The following code illustrates how to use jquery to disable context menus:

    1. $ (Document). Ready (function (){
    2. $ (Document). BIND ("contextmenu", function (e ){
    3. Return false;
    4. });
    5. });

3. add and delete CSS classes in jquery

Adding and deleting CSS classes in jquery is very simple:

    1. // To add a CSS class, you can use the following piece of code
    2. If ($ (ID). hasclass ('testclass '))
    3. {
    4. $ ('# Div1'). addclass ('testclass ');
    5. }
    6.  
    7. // To remove a CSS class, you can use the following piece of code
    8. If ($ (ID). hasclass ('testclass '))
    9. {
    10. $ ('# Div1'). removeclass ('testclass ');
    11. }

4. Check whether an element is available.

You can use jquery to check whether an element in a webpage exists. The following is an example:

  1. If($ ('Img'). Length)
  2. {
  3. Alert ('Image elements available');
  4. }
  5. Else
  6. {
  7. Alert ('Image elements not available');
  8. }

5. Use jquery to check the browser type

Different browsers use different methods to execute scripts, but you can use jquery to easily identify the browser type and then execute the corresponding custom code. The following shows how to use jquery to check the browser code snippet:

  1. If(Jquery. browser. Mozilla)
  2. {
  3. // Code to execute if browser is Mozilla 
  4. }
  5. If(Jquery. browser. MSIE)
  6. {
  7. // Code to execute if browser is IE 
  8. }
  9. If(Jquery. browser. Safari)
  10. {
  11. // Code to execute if browser is Safari 
  12. }
  13. If(Jquery. browser. Opera)
  14. {
  15. // Code to execute if browser is opera 
  16. }

6. Use jquery to discover hidden elements

You can use size () to check hidden DOM elements. The following is an example:

    1. If($ ("Div. Hidden"). Size)
    2. {
    3. Alert (One or more divs are den');
    4. }

You can also use the length () function to implement the same result. In fact, the size () function also calls the length () function, so the length () function is faster.

    1. If($ ("Div. Hidden"). Length)
    2. {
    3. Alert (One or more divs are den');
    4. }

7. Save data in the DOM

You can use the data () function to save data in the DOM element. The following code snippet shows how to assign values to a DOM element using jquery:

    1. $ ('# Div1'). Data ('Key','Value');

To retrieve the data stored in the DOM element, you can use the following code:

    1. $ ('# Div1'). Data ('key ');

8. Retrieve the parent element of an element

It is very easy to use jquery to check the parent element of an element. You need to call the closest () function as follows:

    1. VaR id = $ ("Btnhello"). Closest ("Div"). ATTR ("ID");

9. Correctly Use the linked list in jquery

The chain table (chaining) is a great function in jquery, which promotes the actions in the chain table to be executed one after another. You can use it using the chain table method. The following code is an example:

    1. $ ('Div1'). Removeclass ('Color'). Addclass ('No-color');

10. Use jquery to select a list

Jquery makes it easier to use the selection list. You can easily delete a list item from the selection list. The specific method is as follows:

    1. $ ("# Employeelist option [value = '9']"). Remove ();

The following code illustrates how to retrieve a selection item in text format from the selection list:

    1. $ ('# Employeelist: selected'). Text ();

Summary

Jquery is a powerful JavaScript library that simplifies cross-browser, client scripts, event processing, animation, Dom traversal, and Ajax development, the 10 jquery-related tips presented in this article can help you make good use of them. You are welcome to share some useful jquery usage tips.

Original article title: 10 jquery tips and tricks for Visual Studio

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.