JQuery BASICS (2) $ and jQuery basics in jquery

Source: Internet
Author: User

JQuery BASICS (2) $ and jQuery basics in jquery

In jQuery, $ is the most commonly used symbol, which provides a variety of functions. This includes selecting one or more elements on the page, prefix of functional functions, perfection of windows. onload, and creation of DOM nodes. This document describes how to use jQuery. As the foundation

1. Selector

In css, selector is used to select a class (Category selector) element or a class (id selector) of a page ). In jQuery, "$" is used as a selector to select a certain type or element, but jQuery provides a more comprehensive selection method. It also handles browser compatibility issues for users.

For example, the <a> element in css

H2 a {/* Add css attributes */}

In jQuery, use the following code to select element a under h2 as an object array for javascript.

$("H2 a")

Example:

<Body> <script type = "text/javascript"> window. onload = function () {var oElements = $("h2 a"); // select the Matching Element for (var I = 0; I <oElements. length; I ++) oElements [I]. innerHTML = I. toString ();} </script> 

As shown above, there are five <H2> tags in the document that contain <a> elements. The $ selector is used, as shown below. jQuery can easily select elements. If DOM is used, A node like this will use a lot of javascript code.

The general syntax of selector in jQuery is as follows:

$(selector)

Or

jQuery(selector)

Among them, selector complies with css standards, and even css3 standards.

For example:

$("#showDiv")

Id selector, which is equivalent to document. getElementById ("shouDiv") in javascript. It can be seen that jQuery's color changing method is much simpler.

$(".newClass")

Class selector: select the node where the css class is "newClass". To achieve the same choice in javascript, use the for loop to traverse the entire DOM.

$("p:odd")

Select the p element of all odd rows, and use ": odd" and ": even" for almost all tags.

$("td:nth-child(1)")

Select the first cell of all table rows, that is, the first column. This is very useful when modifying a column in a table. You do not need to traverse the table by row.

$("li>a")

Child selector. The <a> element marked under <li> is returned, excluding the sun element tag.

$("a[herf$=pdf]")

Property selector, select all hyperlinks, and these links end with pdf. With the attribute selector, you can select the elements of various features on the page.

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.