Niuke's Jquery -- mutual conversion between Jquery and DOM objects and three DOM operations _ jquery

Source: Internet
Author: User
Jquery objects are generated after DOM objects are packaged by jQuery. jQuery objects are unique to jQuery. They can be used in jQuery, this article introduces Jquery objects that are generated after DOM objects are packaged by jQuery. jQuery objects are unique to jQuery and can be used in jQuery. Interested friends can refer to the various functions of the jQuery class library that can be called only by jQuery objects. Similarly, some dom object attributes and methods cannot be called on jQuery, however, basically the functions provided by the jQuery Class Library contain all dom operations. This requires us to know how to convert jQuery objects and DOM.

1. jQuery objects are the objects generated after DOM objects are encapsulated by jQuery.

2. Mutual conversion between jQuery objects and DOM objects.

Good writing style:

The Code is as follows:


Var $ input = $ ("input ")

Add $ to the object obtained by jQuery.

<1> convert a jQUery object to a DOM object. Two methods are available: [index] And get (index)

A: var $ cr = $ ("# cr") // jQuery object
Var cr = $ cr [0] // DOM object
B: var $ cr = $ ("# cr") // jQuery object
Var cr = $ cr. get (0); // DOM object

<2> convert a DOM object to a jQuery object

Var cr = document. getElementById ("cr"); // DOM object
Var $ cr = $ (cr );

3. Resolve conflicts with other databases

JQuery. noConflict ().
JQuery uses $ as its own shortcut.

4. advantages of using jQuery

<1> concise writing
<2> support for ccs3.
<3> improved processing mechanism

An error is reported when you run the preceding code browser!
But if you write it like this:

The Code is as follows:


(('{Tt'}.css ("color", "red ");


The browser will not report an error because it does not have this element!

5. jQuery Selector

JQuery selector is the top priority of jQuery!

The jQuery filter selector is similar to the pseudo class selector in CSS.

<1> even and odd Selector

Even number: $ ("tr: even ")
Odd: $ ("tr: odd ")

<2> CSS3 pseudo-class selector parity

The Code is as follows:


P: nth-child (odd)
{
Background: # ff0000;
}
P: nth-child (even)
{
Background: # 0000ff;
}

<2> form type Selector

<3> escape selector prevents errors

6. DOM operation Classification (1: DOM Core 2. HTML-DOM 3. CSS-DOM)

1. DOM Core

DOM Core is not specialized in JavaScript. It can be used by any programming language that supports DOM. Its purpose is not limited to processing web pages. It can also be used to process any document written in a markup language, for example, XML.

2. HTML_DOM

Many HTML--DOM-specific properties exist when scripting HTML files with JavaScript and DOM.
HTML_DOM provides some more concise marks to describe the attributes of various HTML elements.
For example:

The Code is as follows:


Document. forms
Element. src


Can only be used for WEB

3. CSS_DOM

CSS_DOM is a CSS operation. It mainly obtains and sets various attributes of the style object.
You can change the attributes of a style object. Change the effect.

The Code is as follows:


Element. style. color = "red ";


7. Traverse nodes

1. children ()
2. next ()
3. prev ()
4. siblings ()
5. closest ()

8. jquey css

<1> you can use opacity to set transparency. jQuery has handled compatibility issues.

$ ("P" 2.16.css ("opacity", "0.5 ");

<2> $ ("p"). height (100) // 100 the default unit is px. To use another unit, you must use a string.

<3> offset () method

Returns the offset relative to the window.

The Code is as follows:


Var offset = $ ("p"). offset ();
Var left = offset. left;
Var top = offset. top;

<4> position ()

The Code is as follows:


// Returns the offset relative to the latest position style.
Var position = $ ("p"). position ();
Var left = position. left;
Var top = position. top;


<5> scrollTop () and scrollLeft ()

The Code is as follows:


// Return the distance from the top of the scroll bar to the left.
Var $ p = $ ("p ");
Var top = $ p. scrollTop ();
Var left = $ p. scrollLeft ();
// You can also set the scroll to the specified position:
$ ("AB"). scrollTop (300 );

<6> pageX and pageY are used to obtain the cursor position on the page.

The Code is as follows:


$ (Document). mousemove (function (e ){
$ ("Span"). text ("X:" + e. pageX + ", Y:" + e. pageY );
});

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.