Home Web Front-end JS Tutorial jQuery User Manual (2)_jquery

jQuery User Manual (2)_jquery

May 16, 2016 pm 06:45 PM
jquery manual

二: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插入到在匹配元素后
<p id="test">afterp><a href="#" onClick="jq()">jQuerya>
jQuery代码及功能
function jq(){  
     $(
"a").after($("#test"));  
}
执行后相当于
<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)相反
<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() 删除匹配对象的所有子节点
<div id="test">
  
<span>spanspan>
  
<p>afterp>
div>
<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()  删除匹配对象
注意区分empty(),empty()移出匹配对象的子节点,remove(),移出匹配对象

wrap(htm) 将匹配对象包含在给出的html代码内
<p>Test Paragraph.p> <a href="#" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){  
      $(
"p").wrap("
"); 
}
执行后相当于
<div class='wrap'><p>Test Paragraph.p>div>

wrap(elem) 将匹配对象包含在给出的对象内
<p>Test Paragraph.p><div id="content">div>
<a href="#" onClick="jq()">jQuerya>
jQuery代码及功能:

function jq(){  
      $(
"p"
).wrap( document.getElementById('content') );
}

执行后相当于

<div id="content"><p>Test Paragraph.p>div>


遍历、组合
add(expr)  在原对象的基础上在附加符合指定表达式的jquery对象

<p>Hellop><p><span>Hello Againspan>p>
<a href="#" onClick="jq()">jQuerya>
jQuery代码及功能:
function jq(){
     
var f=$("p").add("span");    
     
for(var i=0;i < $(f).size();i++){
alert($(f).eq(i).html());}
}
执行$("p")得到匹配

的对象,有两个,add("span")是在("p")的基础上加上匹配的对象,所有一共有3个,从上面的函数运行结果可以看到$("p").add("span")是3个对象的集合,分别是 [

Hello

],[

Hello Again

],[Hello Again]。

add(el)  在匹配对象的基础上在附加指定的dom元素。
        $("p").add(document.getElementById("a"));

add(els)  在匹配对象的基础上在附加指定的一组对象,els是一个数组
<p>Hellop><p><span>Hello Againspan>p>
jQuery代码及功能:
function jq(){
     
var f=$("p").add([document.getElementById("a"), document.getElementById("b")])
     
for(var i=0;i < $(f).size();i++){
             alert($(f).eq(i).html());}
}
注意els是一个数组,这里的[ ]不能漏掉。

ancestors ()  一依次以匹配结点的父节点的内容为对象,根节点除外(有点不好理解,看看下面例子就明白了)
<div>
    
<p>onep>
    
<span>
    
<u>twou>
    
span>
div>
jQuery代码及功能:
function jq(){
     
var f= $("u").ancestors();
     
for(var i=0;i < $(f).size();i++){
alert($(f).eq(i).html());}
}
第一个对象是以的父节点的内容为对象,[ two ]
第一个对象是以的父节点的父节点(div)的内容为对象,[

one

two ]
一般一个文档还有和,依次类推下去。

ancestors (expr)  在ancestors()的基础上之取符合表达式的对象
如上各例子讲var f改为var f= $("u").ancestors(“div”),则只返回一个对象:
[

one

two  ]

children()  返回匹配对象的子介点
<p>onep>
<div id="ch">   
     
<span>twospan>
div>
jQuery代码及功能:
function jq(){
    alert($(
"#ch").children().html());
}
$("#ch").children()得到对象[ two ].所以.html()的结果是”two”

children(expr)  返回匹配对象的子介点中符合表达式的节点
<div id="ch">   
      
<span>twospan>
      
<span id="sp">threespan>
div>
jQuery代码及功能
function jq(){
    alert($(
"#ch").children(“#sp”).html());
}
$("#ch").children()得到对象[twothree ].
$("#ch").children(“#sp”)过滤得到[three ]

parent ()  parent (expr)取匹配对象父节点的。参照children帮助理解

contains(str)  返回匹配对象中包含字符串str的对象
<p>This is just a test.p><p>So is thisp>
jQuery代码及功能:
function jq(){
    alert($(
"p").contains("test").html());
}
$("p")得到两个对象,而包含字符串”test”只有一个。所有$("p").contains("test")返回 [

This is just a test.

]

end() 结束操作,返回到匹配元素清单上操作前的状态.

filter(expr)   filter(exprs)   过滤现实匹配符合表达式的对象 exprs为数组,注意添加“[ ]”
<p>Hellop><p>Hello Againp><p class="selected">And Againp>
jQuery代码及功能:
function jq(){
    alert($(
"p").filter(".selected").html())
}
$("p")得到三个对象,$("p").contains("test")只返回class为selected的对象。

find(expr)  在匹配的对象中继续查找符合表达式的对象
<p>Hellop><p id="a">Hello Againp><p class="selected">And Againp>
Query代码及功能:
function jq(){
    alert($(
"p").find("#a").html())
}
在$("p")对象中查找id为a的对象。

is(expr)  判断对象是否符合表达式,返回boolen值
<p>Hellop><p id="a">Hello Againp><p class="selected">And Againp>
Query代码及功能:
function jq(){
    alert($(
"#a").is("p"));
}
Does $("#a ") conform to the jquery expression.
You can use $("#a").is("div"); ("#a").is("#a") to test it more

next( ) next(expr) Returns the remaining sibling nodes of the matching object
<p>Hellop><p id="a">Hello Againp><p class="selected">And Againp>
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)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

How to view the user manual of DingTalk PC version - How to view the user manual of DingTalk PC version How to view the user manual of DingTalk PC version - How to view the user manual of DingTalk PC version Mar 06, 2024 am 11:37 AM

Recently, some friends have asked me how to view the user manual of the DingTalk PC version. Here is how to view the user manual of the DingTalk PC version. Friends who are in need can come and learn more. Step 1: Click to log in to DingTalk PC version. Step 2: Then, after entering the home page, click the "Function" column. Step 3: In the expanded function bar, click DingTalk Secretary. Step 4: Enter the little secretary's page and click "Usage Guide" in the lower left corner. Step 5: After clicking on the user guide, click "User Manual" to view the manual in detail.

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

See all articles