English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery HTML/CSS Methods

A very important part of jQuery is the ability to manipulate DOM elements.

jQuery provides effective methods for operating on HTML elements, attributes, and CSS.

Class attributes

These methods check and operate on the class attributes assigned to the element:

MethodDescription
addClass()将一个或多个类名称添加到所选元素
Add one or more class names to the selected elementhasClass()
Check if any selected elements have the specified class nameremoveClass()
Remove one or more classes from the selected elementtoggleClass()/In addition to

Toggle one or more classes between the selected element

Copying in

DOM insertion-Create a deep copy of the selected element

Surrounding

DOM insertion-Wrap each selected element's content with an HTML element

Internal

MethodDescription
These methods allow us to insert new content into the existing element:Insert content at the end of the selected element
appendTo()Insert an HTML element at the end of the selected element
html()Set or get the HTML content of the selected element
prepend()Insert content at the beginning of the selected element
prependTo()Insert an HTML element at the beginning of the selected element
text()Set or get the text content of the selected element

DOM insertion-External

These methods allow us to insert new content outside the existing element:

MethodDescription
after()Insert content after the selected element
before()Insert content before the selected element
insertAfter()Insert an HTML element after the selected element
insertBefore()Insert an HTML element before the selected element

DOM removal

These methods allow us to remove elements from the DOM:

MethodDescription
detach()Remove the selected element (retaining data and events)
empty()Remove all child nodes from the selected element (including text nodes)
remove()Remove the selected element (including data and events)
unwrap()Remove the parent element of the selected element

DOM replacement

These methods are used to remove content from the DOM and replace it with new content:

MethodDescription
replaceAll()Replace the selected element with a new HTML element
replaceWith()Replace the selected element with new content

General attributes

These methods get and set the DOM attributes of the element:

MethodDescription
attr()Set or return the attribute of the selected element/Value
prop()Set or return the attribute of the selected element/Value
removeAttr()Remove one or more attributes from the selected element
removeProp()Remove attributes set by the prop() method
val()Set or return the value attribute of the selected element (for form elements)

Style Methods

These methods get and set CSS-related properties of elements:

MethodDescription
css()Set or return one or more style properties for the selected element
height()Set or return the height of the selected element
innerHeight()Set or return the height of the element (including padding, but not including border)
innerWidth()Set or return the width of the element (including padding, but not including border)
offset()Set or return the offset coordinates of the selected element (relative to the document)
offsetParent()Return the first positioned parent element
outerHeight()Set or return the height of the element (including padding, border, and optional margin)
outerWidth()Set or return the width of the element (including padding, border, and optional margin)
position()Return the position of the element relative to the offset parent
scrollLeft()Set or return the horizontal scroll bar position of the selected element
scrollTop()Set or return the vertical scroll bar position of the selected element
width()Set or return the width of the selected element