Home Web Front-end JS Tutorial jQuery User Manual Part 2 DOM Operation_jquery

jQuery User Manual Part 2 DOM Operation_jquery

May 16, 2016 pm 07:16 PM
DOM operation

属性
我们以为例,在原始的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插入到在匹配元素后
jQuery User Manual Part 2 DOM Operation_jquery<p id="test">afterp><a href="#" onClick="jq()">jQuerya>
jQuery代码及功能
function jq(){  
     $(
"a").after($("#test"));  
}
执行后相当于
jQuery User Manual Part 2 DOM Operation_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)相反
jQuery User Manual Part 2 DOM Operation_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() 删除匹配对象的所有子节点
jQuery User Manual Part 2 DOM Operation_jquery<div id="test">
jQuery User Manual Part 2 DOM Operation_jquery  
<span>spanspan>
jQuery User Manual Part 2 DOM Operation_jquery  
<p>afterp>
jQuery User Manual Part 2 DOM Operation_jquery
div>
jQuery User Manual Part 2 DOM Operation_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()  删除匹配对象
注意区分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是一个数组
jQuery User Manual Part 2 DOM Operation_jquery<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的对象
jQuery User Manual Part 2 DOM Operation_jquery<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"));
}
在$("#a ")是否符合jquery表达式。
大家可以用$("#a").is("div");  ("#a").is("#a")多来测试一下

next()  next(expr)  返回匹配对象剩余的兄弟节点
<p>Hellop><p id="a">Hello Againp><p class="selected">And Againp>
jQuery代码及功能
function jq(){
        alert($(
"p").next().html());
        alert($(
"p").next(".selected").html());
}
$("p").next() returns [

Hello Again

,

And Again

] two objects
$("p").next(".selected) only returns [

And Again

] an object

prev () prev (expr) Refer to next to understand

not(el) not(expr) Remove the matching object from the jQuery object, el is the dom element, expr is the jQuery expression
<p>onep><p id="a">twop>
<a href ="#" onclick="js()">jQuery a>
jQuery code and functions:
function js(){
alert($(
"p").not(document.getElementById("a")).html());
alert($ (
"p").not(“#a”).html());
}
$("p") consists of two objects, and the excluded object is [

one

]

siblings () siblings (expr) jquery Match other sibling-level objects in the object
<p>onep>
<div>
<p id="a">two< /p>
div>
<a href="#" onclick ="js()">jQuery
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

See all articles