Home Web Front-end JS Tutorial Detailed explanation of jQuery object and DOM object conversion method_jquery

Detailed explanation of jQuery object and DOM object conversion method_jquery

May 16, 2016 am 09:00 AM
Convert

this article analyzes the conversion method between jquery objects and dom objects with examples. share it with everyone for your reference, the details are as follows:

convert jquery object to dom object

only jquery objects can call various functions of the jquery class library. similarly, some properties and methods of dom objects cannot be called on jquery, but basically the functions provided by the jquery class library include all dom operations. sometimes, especially when you are new to jquery and cannot remember all the functions of jquery, you will use jquery selectors and original dom functions for development for a long time. therefore, the conversion of the two objects is necessary.

the index of the jquery object saves the dom object, so the jquery object can be converted into a dom object through the index (actually, the dom object saved in the jquery object is obtained).

$("#myphoto")[0];

Copy after login

after returning the dom object through the index, you can use various methods and attributes of the dom object, such as getting the src attribute of the dom object:

alert($("#myphoto")[0].src);

Copy after login

if you want to iterate through each element in a jquery object, you usually use the each() function.

echo(callback);

Copy after login

callback() is a callback function, and this in this function also points to the dom element.

$("#myphoto").each(function(i){
     this,src="test"+i+".jpg";
});

Copy after login

a little tip for lazy people, if you don’t want to remember whether this is a jquery object or this object in different jquery functions, you can use the "this" method to convert both into jquery objects, because even an object is already a jquery object you can't go wrong either.

convert dom object to jquery object

if you have already obtained a dom object, you can use the "jquery(elements)" function to convert it into a jquery object:

var img=document.getelementbyid("myphoto");
jquery(img).css("border","solid 2px #ff0000");

Copy after login

in the above code, img is the dom object obtained using dom. after converting it into a jquery object, you can use the css() method of the jquery object to change its style.

you can use "$" instead of "jquery" because jquery has the following implementation inside:

jquery=window.jquery=window.$

Copy after login

the "$" character can be used as a variable name in javascript and can appear as a prefix. but some other libraries or programs may already use "$" as variable names.

jQuery(img).css("border","solid 2px #FF0000");
$(img). css("border","solid 2px #FF0000");

Copy after login

the above two statements are equivalent.

the elements parameter of the "jquery(elements)" function can also be a jquery object. although it does not make sense to convert a jquery object this time, this is so that when it is not sure whether the type of an object is a jquery object or a dom object, it can be converted again call this function for conversion, which ensures that this object must be a jquery object.

readers who are interested in more jquery-related content can check out the special topics on this site: "summary of jquery's methods of operating dom nodes" "jquery common operation skills summary", "jquery summary of common event usage and techniques", "jquery operation json data summary", "jquery xml operation skills summary" and "jquery extension skills summary

i hope this article will be helpful to everyone in jquery programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A simple guide to converting USDT ERC20 to TRC20 A simple guide to converting USDT ERC20 to TRC20 Jan 18, 2024 pm 06:09 PM

A simple guide to converting USDT ERC20 to TRC20

Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width form

How to convert ODT to Word in Windows 11/10? How to convert ODT to Word in Windows 11/10? Feb 20, 2024 pm 12:21 PM

How to convert ODT to Word in Windows 11/10?

How to convert AI files to CDR format How to convert AI files to CDR format Feb 19, 2024 pm 04:09 PM

How to convert AI files to CDR format

How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone Mar 21, 2024 pm 01:21 PM

How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time processing: How to convert timestamp to string in Golang

How to convert a virtual machine to a physical machine? How to convert a virtual machine to a physical machine? Feb 19, 2024 am 11:40 AM

How to convert a virtual machine to a physical machine?

Detailed explanation of the implementation method of converting PHP months to English months Detailed explanation of the implementation method of converting PHP months to English months Mar 21, 2024 pm 06:45 PM

Detailed explanation of the implementation method of converting PHP months to English months

See all articles