search
HomeWeb Front-endJS TutorialTranslated jQuery query manual_jquery

翻译整理:Young.J
官方网站:
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/

   下载完成后先加载到文档中,然后我们来看个简单的例子!

script language="javascript" type="text/javascript">   
    $(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前:

Translated jQuery query manual_jqueryp>onep>
Translated jQuery query manual_jquery
div>
Translated jQuery query manual_jquery     
p>twop>
Translated jQuery query manual_jquery
div>
    p>threep> 
    href="#" id="test" onClick="jq()" >jQuerya>

jQuery代码及功能:
function jq(){  
    alert($(
"div > p").html());  
}
运行:当点击id为test的元素时,弹出对话框文字为two,即div标签下p元素的内容
function jq(){
    $(
"

Hello

").appendTo("body");
}
运行:当点击id为test的元素时,向body中添加“

Hello



$(elem)
说明:限制jQuery作用于一个特定的dom元素,这个函数也接受xml文档和windows对象
参数: elem:通过jQuery对象压缩的DOM元素
例子:
未执行jQuery前:
p>onep>
  
div>
     
p>twop>
  
div>p>threep>
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
    alert($(document).find(
"div > p").html());
}
运行:当点击id为test的元素时,弹出对话框文字为two,即div标签下p元素的内容
function jq(){
   $(document.body).background(
"black");
}
运行:当点击id为test的元素时,背景色变成黑色

$(elems)
说明:限制jQuery作用于一组特定的DOM元素
参数: elem:一组通过jQuery对象压缩的DOM元素
例子:
未执行jQuery前:
Translated jQuery query manual_jqueryform id="form1">
Translated jQuery query manual_jquery     
input type="text" name="textfield">
Translated jQuery query manual_jquery     
input type="submit" name="Submit" value="提交">
Translated jQuery query manual_jquery
form>
Translated jQuery query manual_jquery
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){ 
   $(form1.elements ).hide(); 
}
运行:当点击id为test的元素时,隐藏form1表单中的所有元素。

$(fn)
说明:$(document).ready()的一个速记方式,当文档全部载入时执行函数。可以有多个$(fn)当文档载入时,同时执行所有函数!
参数:fn (Function):当文档载入时执行的函数!
例子:
$( function(){
    $(document.body).background(
"black");
})
运行:当文档载入时背景变成黑色,相当于onLoad。

$(obj)
说明:复制一个jQuery对象,
参数:obj (jQuery): 要复制的jQuery对象
例子:
未执行jQuery前:
p>onep>
div>
   
p>twop>
div>
p>threep>
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
    
var f = $("div"); 
    alert($(f).find(
"p").html()) 
}
运行:当点击id为test的元素时,弹出对话框文字为two,即div标签下p元素的内容。

each(fn)
说明:将函数作用于所有匹配的对象上
参数:fn (Function): 需要执行的函数
例子:
未执行jQuery前:
img src="1.jpg"/>
img src="1.jpg"/>
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
   $(
"img").each(function(){ 
        
this.src = "2.jpg"; });
}
运行:当点击id为test的元素时,img标签的src都变成了2.jpg。

eq(pos)
说明:减少匹配对象到一个单独得dom元素
参数:pos (Number): 期望限制的索引,从0 开始
例子:
未执行jQuery前:
Translated jQuery query manual_jqueryp>This is just a test.p>
Translated jQuery query manual_jquery
p>So is thisp>
Translated jQuery query manual_jquery
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
    alert($(
"p").eq(1).html())
}
运行:当点击id为test的元素时,alert对话框显示:So is this,即第二个

标签的内容


get() get(num)
说明:获取匹配元素,get(num)返回匹配元素中的某一个元素
参数:get (Number): 期望限制的索引,从0 开始
例子:
未执行jQuery前:
p>This is just a test.p>
p>So is thisp>
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
    alert($(
"p").get(1).innerHTML);
}
运行:当点击id为test的元素时,alert对话框显示:So is this,即第二个

标签的内容

注意get和eq的区别,eq返回的是jQuery对象,get返回的是所匹配的dom对象,所有取$("p").eq(1)对象的内容用jQuery方法html(),而取$("p").get(1)的内容用innerHTML

index(obj)
说明:返回对象索引
参数:obj (Object): 要查找的对象
例子:
未执行jQuery前:
div id="test1">div>
div id="test2">div>
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
    alert($(
"div").index(document.getElementById('test1')));
    alert($(
"div").index(document.getElementById('test2')));
}
运行:当点击id为test的元素时,两次弹出alert对话框分别显示0,1

size()   Length
说明:当前匹配对象的数量,两者等价
例子:
未执行jQuery前:
img src="test1.jpg"/>
img src="test2.jpg"/>
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
    alert($(
"img").length);
}
运行:当点击id为test的元素时,弹出alert对话框显示2,表示找到两个匹配对象
 


                                                   二:DOM操作
属性
我们以Translated jQuery query manual_jquery为例,在原始的javascript里面可以用var o=document.getElementById('a')取的id为a的节点对象,在用o.src来取得或修改该节点的scr属性,在jQuery里$("#a")将得到jQuery对象[ Translated jQuery query manual_jquery ],然后可以用jQuery提供的很多方法来进行操作,如$("#a").scr()将得到5.jpg,$("#a").scr("1.jpg")将该对象src属性改为1,jpg。下面我们来讲jQuery提供的众多jQuery方法,方便大家快速对DOM对象进行操作
herf()   herf(val)
说明:对jQuery对象属性herf的操作。
例子:
未执行jQuery前
href="1.htm" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
   alert($(
"#test").href());
   $(
"#test").href("2.html");
}
运行:先弹出对话框显示id为test的连接url,在将其url改为2.html,当弹出对话框后会看到转向到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
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){  
       $(
"#test").after("Hello");  
}
执行后相当于:
href="#" id="test" onClick="jq()">jQuerya>b>Hellob>

after(elem)  after(elems)  将指定对象elem或对象组elems插入到在匹配元素后
Translated jQuery query manual_jqueryid="test">afterp>href="#" onClick="jq()">jQuerya>
jQuery代码及功能
function jq(){  
     $(
"a").after($("#test"));  
}
执行后相当于
Translated jQuery query manual_jqueryhref="#" onClick="jq()">jQuerya>id="test">afterp>

append(html)在匹配元素内部,且末尾插入指定html
href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){ 
     $("#test").append("
b>Hellob>");  
}
执行后相当于
href="#" onClick="jq()">jQueryb>Hellob>a>
同理还有append(elem)  append(elems) before(html) before(elem) before(elems)请执行参照append和after的方来测试、理解!

appendTo(expr)  与append(elem)相反
Translated jQuery query manual_jqueryid="test">afterp>href="#" onClick="jq()">jQuerya>
jQuery代码及功能
function jq(){  
      $(
"a"). appendTo ($("#test"));  
}
执行后相当于
id="test">afterhref="#" onClick="jq()">jQuerya> p>

clone() 复制一个jQuery对象
id="test">afterp>href="#" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){  
     $(
"#test").clone().appendTo($("a"));  
}
复制$("#test")然后插入到后,执行后相当于
id="test">afterp>href="#" onClick="jq()">jQuerya>id="test">afterp>

empty() 删除匹配对象的所有子节点
Translated jQuery query manual_jquerydiv id="test">
Translated jQuery query manual_jquery  
span>spanspan>
Translated jQuery query manual_jquery  
p>afterp>
Translated jQuery query manual_jquery
div>
Translated jQuery query manual_jquery
href="#" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){  
    $(
"#test").empty();  
}
执行后相当于
div id="test">div>href="#" onClick="jq()">jQuerya>

insertAfter(expr)   insertBefore(expr)
     按照官方的解释和我的几个简单测试insertAfter(expr)相当于before(elem),insertBefore(expr)相当于after (elem)

prepend (html)  prepend (elem)  prepend (elems)   在匹配元素的内部且开始出插入
通过下面例子区分append(elem)  appendTo(expr)  prepend (elem)
id="a">pp>
div>divdiv>
执行$("#a").append($("div")) 后相当于
id="a">

  
div>divdiv>
p>
执行$("#a").appendTo($("div")) 后 相当于
div>
   div
   
id="a">pp>
div>
执行$("#a").prepend ($("div")) 后 相当于
id="a">
   
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
p>Test Paragraph.p> a href="#" onClick="jq()">jQuerya >
jQuery code and functions:
Statement
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
Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use