Home > Web Front-end > JS Tutorial > body text

Summary of jQuery methods (with examples)

不言
Release: 2019-03-20 11:53:15
forward
2243 people have browsed it

This article brings you a summary of jQuery methods (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Introduction to jQuery

jQuery is a lightweight JS library for operating DOM. It mainly includes the following functions:

HTML element selection and operation

HTML events Function

HTML DOM traversal and modification

CSS operation

JavaScript special effects and animation

AJAX

jQuery-based plug-in

The advantage of jQuery is that it is compatible with all major browsers, including Internet Explorer 6!

jQuery syntax

$(selector).action()

Document is loaded The difference between event

$(document).ready(function() {
  // 代码...
});

// 简写方式
$(function() {
  // 代码...
});
Copy after login

$(document).ready and window.onload

$(document).ready and window.onload are both on page loading Functions that are fully executed are not much different in most cases.
$(document).ready: It is executed after the DOM structure is drawn, without having to wait until it is loaded. This means that it will be executed after the DOM tree is loaded, without waiting for the images or other external files in the page to be loaded. And you can write multiple .ready.
window.onload: All elements of the page have been loaded, including pictures and other elements. Can only be executed once.

jQuery selector

jQuery selector is based on the existing CSS selector

$('*')
$('p ')
$('ul li')
$('ul li:last-child')
...

jQuery Event

Mouse eventsKeyboard eventsForm eventsDocument/Window events
clickkeypresssubmitload
dbclickkeydownchangeresize
mouseenterkeyupfocusscroll
mouseleave
blurunload
hover


#Commonly used jQuery event methods

    click() Click event
  • dbclick() Double-click event
  • mouseenter() Mouse passing through element event
  • mouseleave() Mouse leaving element event
  • mousedown() Move the mouse over the element and press the mouse event
  • mouseup() Hold down the mouse and move it over the element and release the mouse event
  • hover() Mouse hover event
  • focus() Form element focus event
  • blur() Form element loses focus event
  • submit() Form submission event
  • change() Form element value change event
  • keypress() Keyboard key press event
  • keydown() Keyboard key press event
  • keyup() Keyboard key release event
  • load() Specified element loading completion execution event (obsolete after version 1.8)
  • resize() window size change event
  • scroll() scroll listening event

jQuery effect

$(selector).action(speed,callback)
VariableDescriptionselectorSelectoractionEventspeedSpeed, milliseconds, can also be 'slow', 'fast'##callbackShow hide
Callback function

hide() hide the element
  • show() show the element
  • toggle() show the element being Hidden elements, hide displayed elements
  • Fade in and out

fadeIn() Fade in
  • fadeOut() Fade out
  • fadeToggle () The faded element fades in, the faded element fades out
  • fadeTo() Fade to the given opacity
  • $(selector).fadeTo(speed,opacity,callback) ;

    The opacity value is between 0 and 1

  • Slide

slideDown() Slide down to expand the element
  • slideDown( ) Slide up to collapse the element
  • slideToggle() Slide up to collapse the expanded element, slide down to display the collapsed element
  • animation

$(selector). animate({params},speed,callback);

ParameterparamsspeedcallbackInstance
DescriptionWhether it is required
Define the css properties that form the animationMust
Speed , milliseconds, can also be 'slow', 'fast'optional
callback functionoptional Select
$("button").click(function() {
  $("p").animate({
    left: "250px",
    opacity: "0.5",
    height: "150px",
    width: "150px"
  });
});
Copy after login

Stop animation

$(selector).stop(stopAll, goToEnd);

ParameterstopAllgoToEnd

jQuery 链(Chaining)

通过 jQuery,可以把动作/方法链接在一起。

Chaining 允许我们在一条语句中运行多个 jQuery 方法(在相同的元素上,浏览器就不必多次查找相同的元素。

$("#p1")
  .css("color", "red")
  .slideUp(2000)
  .slideDown(2000);

// "p1" 元素首先会变为红色,然后向上滑动,再然后向下滑动
Copy after login

jQuery HTML

jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易。

获取内容和属性

获取内容

  • text() 设置或返回所选元素的文本内容
  • html() 设置或返回所选元素的内容(包括 HTML 标记)
  • val() 设置或返回表单字段的值

获取属性

  • attr() 设置或者返回所选的属性的值
// 获取属性
$('#test').attr('href')

// 设置属性
$('#test').attr('href','http://www.baidu.com')

$('#test').attr({
    href: 'http://www.baidu.com',
    title: '百度'
})

// 回掉函数
$('#test').attr('href', function(i, origValue){
    // i 被选元素列表中当前元素的下标
    // origValue 原始值
    return origValue + '/jquery'
  })
Copy after login

添加删除元素

  • append() 在被选元素的结尾插入内容
  • prepend() 在被选元素的开头插入内容
  • after() 在被选元素之后插入内容
  • before() 在被选元素之前插入内容
     
  • remove() 删除被选元素(及其子元素)
  • empty() 从被选元素中删除子元素

jQuery remove() 方法也可接受一个参数,允许您对被删元素进行过滤。该参数可以是任何 jQuery 选择器的语法。下面的代码表示删除所有 p 元素中类名是 italic 的元素

$('p').remove('.italic')

获取并设置 css 类

  • addClass() 向被选元素添加一个或多个类
  • removeClass() 从被选元素删除一个或多个类
  • toggleClass() 对被选元素进行添加/删除类的切换操作
  • css() 设置或返回样式属性
// 返回样式属性
$("p").css("background-color");

// 设置样式属性
$("p").css("background-color", "yellow");
// 或者
$("p").css({ "background-color": "yellow", "font-size": "200%" });
Copy after login

尺寸方法

  • width() 元素宽度
  • height() 元素高度
  • innerWidth() 包含 padding 宽度
  • innerHeight() 包含 padding 高度
  • outerWidth() 包含 padding、border 宽度
  • outerHeight() 包含 padding、border 高度
  • outerWidth(true) 包含 padding、border、margin 宽度
  • outerHeight(true) 包含 padding、border、margin 高度

元素遍历

祖先元素:

  • parent() 返回被选元素的直接父元素,该方法只会向上一级对 DOM 树进行遍历。
  • parents() 返回被选元素的所有祖先元素,它一路向上直到文档的根元素 ()。
  • parentsUntil() parentsUntil() 方法返回介于两个给定元素之间的所有祖先元素。
$(document).ready(function() {
  // p > ul > li > span
  $("span").parentsUntil("p");
  // 返回 ul 和 li
});
Copy after login

后代元素:

  • children() 返回被选元素的所有直接子元素。
  • find() 方法返回被选元素的后代元素,一路向下直到最后一个后代。
$(document).ready(function() {
  $("p").find("span");
});
Copy after login

同胞元素:

  • siblings() 返回被选元素的所有同胞元素。
  • next() 返回被选元素的下一个同胞元素。
  • nextAll() 返回被选元素的所有跟随的同胞元素。
  • nextUntil() 返回介于两个给定参数之间的所有跟随的同胞元素。
  • prev() 返回被选元素的上一个同胞元素。
  • prevAll() 返回被选元素之前的所有的同胞元素。
  • prevUntil() 返回介于两个给定参数之间的所有前方的同胞元素。

元素过滤:

  • first() 返回被选元素的首个元素。
  • last() 返回被选元素的最后一个元素。
  • eq() 返回被选元素中带有指定索引号的元素。
  • filter() 方法允许您规定一个标准。不匹配这个标准的元素会被从集合中删除,匹配的元素会被返回。
  • not() 方法返回不匹配标准的所有元素。

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的jQuery教程视频栏目!


DescriptionWhether it is required
Whether the animation queue should be cleared, the default is falseOptional
Whether the current animation should be completed immediatelyOptional

The above is the detailed content of Summary of jQuery methods (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!