Translated jQuery query manual_jquery
官方网站:http://jquery.com
jQuery是一款同prototype一样优秀js开发库类,特别是对css和XPath的支持,使我们写js变得更加方便!如果你不是个js高手又想写出优 秀的js效果,jQuery可以帮你达到目的!
下载地址:Starterkit (http://jquery.bassistance.de/jquery-starterkit.zip)
jQuery Downloads (http://jquery.com/src/)
下载完成后先加载到文档中,然后我们来看个简单的例子!
$(document).ready(function(){
$("a").click(function() {
alert("Hello world!");
});
});
<script>
上边的效果是点击文档中所有a标签时将弹出对话框,$("a") 是一个jQuery选择器,$本身表示一个jQuery类,所有$()是构造一个jQuery对象,click()是这个对象的方法,同理$(document)也是一个jQuery对象,ready(fn)是$(document)的方法,表示当document全部下载完毕时执行函数。
在进行下面内容之前我还要说明一点$("p")和$("#p")的区别,$("p")表示取所有p标签()的元素,$("#p")表示取id为"p"()的元素.
我将从以下几个内容来讲解jQuery的使用:
1:核心部分
2:DOM操作
3:css操作
4:javascript处理
5:动态效果
6:event事件
7:ajax支持
8:插件程序
一:核心部分
$(expr)
说明:该函数可以通过css选择器,Xpath或html代码来匹配目标元素,所有的jQuery操作都以此为基础
参数:expr:字符串,一个查询表达式或一段html字符串
例子:
未执行jQuery前:




<p>threep>
<a href="#" id="test" onClick="jq()" >jQuerya>
jQuery代码及功能:
alert($("div > p").html());
}
$("
Hello
}
Hello
$(elem)
说明:限制jQuery作用于一个特定的dom元素,这个函数也接受xml文档和windows对象
参数: elem:通过jQuery对象压缩的DOM元素
例子:
未执行jQuery前:
<div>
<p>twop>
div><p>threep>
<a href="#" id="test" onClick="jq()">jQuerya>
alert($(document).find("div > p").html());
}
$(document.body).background("black");
}
$(elems)
说明:限制jQuery作用于一组特定的DOM元素
参数: elem:一组通过jQuery对象压缩的DOM元素
例子:
未执行jQuery前:





$(form1.elements ).hide();
}
$(fn)
说明:$(document).ready()的一个速记方式,当文档全部载入时执行函数。可以有多个$(fn)当文档载入时,同时执行所有函数!
参数:fn (Function):当文档载入时执行的函数!
例子:
$(document.body).background("black");
})
$(obj)
说明:复制一个jQuery对象,
参数:obj (jQuery): 要复制的jQuery对象
例子:
未执行jQuery前:
<div>
<p>twop>
div>
<p>threep>
<a href="#" id="test" onClick="jq()">jQuerya>
var f = $("div");
alert($(f).find("p").html())
}
each(fn)
说明:将函数作用于所有匹配的对象上
参数:fn (Function): 需要执行的函数
例子:
未执行jQuery前:
<img src="1.jpg"/>
<a href="#" id="test" onClick="jq()">jQuerya>
$("img").each(function(){
this.src = "2.jpg"; });
}
eq(pos)
说明:减少匹配对象到一个单独得dom元素
参数:pos (Number): 期望限制的索引,从0 开始
例子:
未执行jQuery前:



alert($("p").eq(1).html())
}
标签的内容
get() get(num)
说明:获取匹配元素,get(num)返回匹配元素中的某一个元素
参数:get (Number): 期望限制的索引,从0 开始
例子:
未执行jQuery前:
<p>So is thisp>
<a href="#" id="test" onClick="jq()">jQuerya>
alert($("p").get(1).innerHTML);
}
标签的内容
index(obj)
说明:返回对象索引
参数:obj (Object): 要查找的对象
例子:
未执行jQuery前:
<div id="test2">div>
<a href="#" id="test" onClick="jq()">jQuerya>
alert($("div").index(document.getElementById('test1')));
alert($("div").index(document.getElementById('test2')));
}
size() Length
说明:当前匹配对象的数量,两者等价
例子:
未执行jQuery前:
<img src="test2.jpg"/>
<a href="#" id="test" onClick="jq()">jQuerya>
alert($("img").length);
}
二:DOM操作
属性
我们以
herf() herf(val)
说明:对jQuery对象属性herf的操作。
例子:
未执行jQuery前
alert($("#test").href());
$("#test").href("2.html");
}
同理,jQuery还提供类似的其他方法,大家可以分别试验一下:
herf() herf(val) html() html(val) id() id (val) name() name (val) rel() rel (val)
src() src (val) title() title (val) val() val(val)
操作
after(html) 在匹配元素后插入一段html
$("#test").after("Hello");
}
after(elem) after(elems) 将指定对象elem或对象组elems插入到在匹配元素后

$("a").after($("#test"));
}

append(html)在匹配元素内部,且末尾插入指定html
$("#test").append("<b>Hellob>");
}
appendTo(expr) 与append(elem)相反

$("a"). appendTo ($("#test"));
}
clone() 复制一个jQuery对象
$("#test").clone().appendTo($("a"));
}
empty() 删除匹配对象的所有子节点





$("#test").empty();
}
insertAfter(expr) insertBefore(expr)
按照官方的解释和我的几个简单测试insertAfter(expr)相当于before(elem),insertBefore(expr)相当于after (elem)
prepend (html) prepend (elem) prepend (elems) 在匹配元素的内部且开始出插入
通过下面例子区分append(elem) appendTo(expr) prepend (elem)
<div>divdiv>
<div>divdiv>
p>
div
<p id="a">pp>
div>
<div>divdiv>
p>
remove() Delete the matching object
Note the difference between empty(), empty() removes the child node of the matching object, remove(), removes the matching object
wrap(htm ) contains the matching object within the given html code

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

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
