


The difference between jquery and javascript (comparison of common methods)_Basic knowledge
jquery is an extension and encapsulation of javascript, which makes javascript more usable and simpler. As people say, jquery uses less code to complete more functions beautifully. Comparison of common methods between JavaScript and JQuery
1. Differences in loading DOM
JavaScript:
window.onload
function first(){
alert('first');
}
function second(){
alert('second');
}
window.onload = first;
window.onload = second;
//Only the second window.onload will be executed; however, it can be improved by the following method:
window.onload = function(){
first();
second() ;
}
Jquery:
$(document).ready()
$(document).ready(){
function first(){
alert('first');
}
function second(){
alert('second');
}
$(document).ready(function(){
first();
}
$(document).ready(function(){
second();
}
//Both two will be executed
}
2. Get ID
JavaScript:
document.getElementById('idName')
JQuery:
$('#idName')
3. Get Class
JavaScript:
JavaScript has no default method of getting class
JQuery:
$('. className')
4. Get TagName
JavaScript:
document.getElementsByTagName('tagName')
JQuery:
$ ('tagName')
5. Create the object and add it to the document
JavaScript:
var para = document.createElement('p');
//Create a p element
document.body.appendElement(para);
//Append the p element as the lastchild child node of the body. If you want to insert the newly created p element into an existing one Before the element, you can use the insertBefore() method
JQuery:
JQuery provides 4 methods for inserting new elements before or after existing elements (inside): append(), appendTo(), prepend(), prependTo().
Format: $(html);
eg, html code:
World!
$('p').append('Hello! ');
//Output:
World!Hello!
$('Hello!').appendTo('p'); //Output: Same as above
$('p').prepend('Hello!');
//Output:
Hello!World!
$('Hello!').prependTo('p');
//Output: Same as above
6. Insert new element
JavaScript:
insertBefore() Syntax format:
parentElement.insertBefore(newElement,targetElement)
eg, insert an img element before a paragraph.
html code:

This is a piece of text
JavaScript code:
var imgs = document.getElementById('imgs' );
var para = document.getElementsByTag('p');
para.parenetNode.insertBefore(imgs,para);
JQuery:
JQuery provides 4 types of new elements Methods for inserting before or after existing elements (external): after(), insertAfter(), before(), insertBefore().
Format: $(html);
eg, html code:
World!
JQuery code
$('p').after(' Hello!');
//Output:
World!
Hello!$('Hello!') . insertAfter ('p');
//Output: Same as above
$('p').before('Hello!');
//Output: Hello!
World!
$('Hello!').insertBefore('p');
//Output: Same as above
7. Copy node
JavaScript:
reference = node.cloneNode(deep)
This method has only one Boolean parameter, and its possible value can only be true or false. This parameter determines whether the child nodes of the copied node are also copied to the new node.
JQuery:
clone() //After copying the node, the copied new element does not have any behavior
clone(true) //Copy the node content and its bound events
Remarks: This method is usually used in combination with appendTo(), prependTo() and other methods.
8. Delete node
JavaScript:
reference = element.removeChild(node)
removeChild() method deletes one from a given element Child node
JQuery:
remove();
remove() method is to remove all matching elements from the DOM. The remove() method can also be used in conjunction with other filter selectors. Very convenient.
eg, remove the li under ul li whose title is not "Hello":
$('ul li').remove(li[title!='Hello']);
empty() ;
The empty() method is used to clear nodes.
9. Wrap node
JavaScript:
JavaScript not available yet
JQuery:
wrap() //Wrap matching elements with Wrap the structured tags of other elements separately
wrapAll() //Wrap all matching elements with one element
wrapInner() //Wrap the sub-content of matching elements with other structured tags
10. Attribute operations: setting attribute nodes, finding attribute nodes
JavaScript:
document.getElementsByTagName('tagName')
JQuery:
Setting and searching attribute nodes in JQuery are both: attr().
$('p').attr('title'); //Get the title attribute of the p element;
$('p').attr('title','My title'); // Set the title attribute of the p element
$('p').attr('title':'My title','class':'myClass'); //When you need to add multiple attributes, you can use "name" :Value" pairs, separated by commas.
11. Replace node
JavaScript:
reference = element.replaceChild(newChild,oldChild)
This method is to replace a given parent element with Replace a child node with another node.
JQuery:
replaceWith(), replaceAll()
eg:
hello
Want to replace with:
Hi
JQuery code:
$('p') .replaceWith('
Hi
');Or it can be written as:
$('
Hi
').replaceAll('p');12. CSS-DOM operation
JavaScript:
Format: element.style.property
CSS-DOM can read and set the properties of the style object. Its disadvantage is that it cannot extract the style information set by external CSS, while JQuery's .css() method can.
Note: If there is a "-" in CSS such as "font-size", use camel case with the first letter lowercase, such as fontSize.
JQuery:
Format: $(selector).css()
css() method to get the style attribute of the element
In addition, JQuery also provides height() and width() respectively Used to obtain the height and width of the element (without units), while css(height) and css(width) return the height and width with units.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
