Home Web Front-end JS Tutorial Translated jQuery query manual_jquery

Translated jQuery query manual_jquery

May 16, 2016 pm 07:17 PM

翻译整理: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_jquery<p>onep>
Translated jQuery query manual_jquery
<div>
Translated jQuery query manual_jquery     
<p>twop>
Translated jQuery query manual_jquery
div>
    <
p>threep> 
    <a 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>
<a 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_jquery<form 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
<a 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>
<a 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"/>
<a 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_jquery<p>This is just a test.p>
Translated jQuery query manual_jquery
<p>So is thisp>
Translated jQuery query manual_jquery
<a 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>
<a 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>
<a 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"/>
<a href="#" id="test" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
    alert($(
"img").length);
}
运行:当点击id为test的元素时,弹出alert对话框显示2,表示找到两个匹配对象
 


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

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

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

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

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

empty() 删除匹配对象的所有子节点
Translated jQuery query manual_jquery<div 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
<a href="#" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){  
    $(
"#test").empty();  
}
执行后相当于
<div id="test">div><a 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)
<p id="a">pp>
<div>divdiv>
执行$("#a").append($("div")) 后相当于
<p id="a">

  
<div>divdiv>
p>
执行$("#a").appendTo($("div")) 后 相当于
<div>
   div
   
<p id="a">pp>
div>
执行$("#a").prepend ($("div")) 后 相当于
<p 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 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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

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...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

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.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

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 using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

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...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

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.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

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/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

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. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

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...

See all articles