


Detailed explanation of the differences between JavaScript and JQuery common methods with examples
jquery is an extension library and encapsulation library for javascript, which makes javascript easier to use and simpler. jQuery is about using less code and completing more functions beautifully.
Comparison examples of common methods between JavaScript and JQuery are as follows:
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 methods:
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(); } //两条均会执行 }
2. Get ID
JavaScript:
document.getElementById('idName')
JQuery:
$('#idName')
3. Get Class
JavaScript:
JavaScript has no default method of obtaining class
JQuery:
$('.className')
4. Get TagName
JavaScript:
document.getElementsByTagName('tagName')
JQuery:
$('tagName')
5, Create object and join In the document
JavaScript:
var para = document.createElement('p'); //创建一个p元素 document.body.appendElement(para); //将p元素追加为body的lastchild子节点,如果想将新创建的p元素插入到已存在的某个元素之前,可以使用insertBefore()方法
JQuery:
JQuery provides 4 methods for inserting new elements before or after existing elements (inside): append(), appendTo( ), prepend(), prependTo().
Format: $(html);
eg, html code:
<p>World!</p> $('p').append('<b>Hello!</b>'); //输出:<p>World!<b>Hello!</b></p> $('<b>Hello!</b>').appendTo('p'); //输出:同上 $('p').prepend('<b>Hello!</b>'); //输出:<p><b>Hello!</b>World! </p> $('<b>Hello!</b>').prependTo('p'); //输出:同上
6. Insert new element
JavaScript:
insertBefore() Syntax format:
parentElement.insertBefore(newElement,targetElement)
eg, insert an img element before a paragraph.
html code:
<img src="image.jpg" id="imgs" /> <p>这是一段文字</p>
JavaScript code:
var imgs = document.getElementById('imgs'); var para = document.getElementsByTag('p'); para.parenetNode.insertBefore(imgs,para);
JQuery:
JQuery provides 4 ways to insert new elements into existing elements (External) before or after methods: after(), insertAfter(), before(), insertBefore().
Format: $(html);
eg, html code:
World!
JQuery code
$('p').after('<b>Hello!</b>'); //输出:<p>World! </p><b>Hello!</b> $('<b>Hello!</b>'). insertAfter ('p'); //输出:同上 $('p').before('<b>Hello!</b>'); //输出:<b>Hello!</b><p>World! </p> $('<b>Hello!</b>').insertBefore('p'); //输出:同上
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
Note: This method is usually used in combination with appendTo(), prependTo() and other methods.
8. Delete node
JavaScript:
reference = element.removeChild(node)
removeChild() method deletes a child node from a given element
JQuery :
remove();
The remove() method is to remove all matching elements from the DOM. The remove() method can also be used in conjunction with other filter selectors, which is very convenient.
eg, remove the li under ul li whose title is not "Hello":
$('ul li').remove(li[title!='Hello']);
empty() ;
empty() method is used to clear nodes.
9. Wrapping node
JavaScript:
No JavaScript yet
JQuery:
wrap() //将匹配元素用其他元素的结构化标记单独包裹起来 wrapAll() //将所有匹配的元素用一个元素包裹起来 wrapInner() //将匹配元素的子内容用其他结构化的标记包裹起来
10. Attribute operation: Set attribute nodes and find attribute nodes
JavaScript:
document.getElementsByTagName('tagName')
JQuery:
Setting and finding attribute nodes in JQuery are both: attr().
$('p').attr('title'); //获取p元素的title属性; $('p').attr('title','My title'); //设置p元素的title属性 $('p').attr('title':'My title','class':'myClass'); //当需要添加多个属性时,可以用"名:值"对的形式,中间用逗号隔开。
11、替换节点
JavaScript:
reference = element.replaceChild(newChild,oldChild)
该方法是将一个给定父元素里的一个子节点替换为另外一个节点。
JQuery:
replaceWith()、replaceAll()
eg:
hello
想替换为:
Hi
JQuery代码:
$('p') .replaceWith('
Hi
');或者可以写成:
$('
Hi
').replaceAll('p');12、CSS-DOM操作
JavaScript:
格式:element.style.property
CSS-DOM能够读取和设置style对象的属性,其不足之处是无法通过它来提取外部CSS设置的样式信息,而JQuery的.css()方法是可以的。
注意点:CSS中的如"font-size"这样有"-"的,要使用首字母小写的驼峰式表示,如fontSize。
JQuery:
格式:$(selector).css()
css()方法获取元素的样式属性
此外,JQuery还提供了height()和width()分别用来获取元素的高度和宽度(均不带单位),而css(height)、css(width)返回高宽,且带单位。
The above is the detailed content of Detailed explanation of the differences between JavaScript and JQuery common methods with examples. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

char and unsigned char are two data types that store character data. The main difference is the way to deal with negative and positive numbers: value range: char signed (-128 to 127), and unsigned char unsigned (0 to 255). Negative number processing: char can store negative numbers, unsigned char cannot. Bit mode: char The highest bit represents the symbol, unsigned char Unsigned bit. Arithmetic operations: char and unsigned char are signed and unsigned types, and their arithmetic operations are different. Compatibility: char and unsigned char

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

C language functions are reusable code blocks. They receive input, perform operations, and return results, which modularly improves reusability and reduces complexity. The internal mechanism of the function includes parameter passing, function execution, and return values. The entire process involves optimization such as function inline. A good function is written following the principle of single responsibility, small number of parameters, naming specifications, and error handling. Pointers combined with functions can achieve more powerful functions, such as modifying external variable values. Function pointers pass functions as parameters or store addresses, and are used to implement dynamic calls to functions. Understanding function features and techniques is the key to writing efficient, maintainable, and easy to understand C programs.

Multithreading is an important technology in computer programming and is used to improve program execution efficiency. In the C language, there are many ways to implement multithreading, including thread libraries, POSIX threads, and Windows API.

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.
