Home>Article>Web Front-end> Summary of jQuery methods (with examples)
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() { // 代码... });
$(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 events | Keyboard events | Form events | Document/Window events |
---|---|---|---|
click | keypress | submit | load |
dbclick | keydown | change | resize |
mouseenter | keyup | focus | scroll |
mouseleave | blur | unload | |
hover |
jQuery effect
$(selector).action(speed,callback)Description | |
---|---|
Selector | |
Event | |
Speed, milliseconds, can also be 'slow', 'fast' | |
Callback function |
$(selector).fadeTo(speed,opacity,callback) ;
The opacity value is between 0 and 1Description | Whether it is required | |
---|---|---|
Define the css properties that form the animation | Must | |
Speed , milliseconds, can also be 'slow', 'fast' | optional | |
callback function | optional Select |
$("button").click(function() { $("p").animate({ left: "250px", opacity: "0.5", height: "150px", width: "150px" }); });
Stop animation
通过 jQuery,可以把动作/方法链接在一起。
Chaining 允许我们在一条语句中运行多个 jQuery 方法(在相同的元素上,浏览器就不必多次查找相同的元素。
$("#p1") .css("color", "red") .slideUp(2000) .slideDown(2000); // "p1" 元素首先会变为红色,然后向上滑动,再然后向下滑动
jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易。
获取内容
获取属性
// 获取属性 $('#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' })
jQuery remove() 方法也可接受一个参数,允许您对被删元素进行过滤。该参数可以是任何 jQuery 选择器的语法。下面的代码表示删除所有 p 元素中类名是 italic 的元素
$('p').remove('.italic')
// 返回样式属性 $("p").css("background-color"); // 设置样式属性 $("p").css("background-color", "yellow"); // 或者 $("p").css({ "background-color": "yellow", "font-size": "200%" });
祖先元素:
$(document).ready(function() { // p > ul > li > span $("span").parentsUntil("p"); // 返回 ul 和 li });
后代元素:
$(document).ready(function() { $("p").find("span"); });
同胞元素:
元素过滤:
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的jQuery教程视频栏目!
Description | Whether it is required | |
---|---|---|
Whether the animation queue should be cleared, the default is false | Optional | |
Whether the current animation should be completed immediately | Optional |
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!