`
jian0487
  • 浏览: 96176 次
  • 性别: Icon_minigender_1
  • 来自: 宁德
社区版块
存档分类
最新评论

jQuery:DOM部分之Traversing 的使用

阅读更多

也就是DOM元素的遍历

函数:add(expr)

功能:添加匹配表达式

返回:jQuery对象

参数:expr (String): An expression whose matched elements are added 

例子:

jQuery Code

$("p").add("span")

Before

(HTML) <p>Hello</p><span>Hello Again</span>

Result:

(jQuery object matching 2 elements) [ <p>Hello</p>, <span>Hello Again</span> ]


函数:add(html)

功能:给匹配元素添加html内容

返回:jQuery对象

参数:html (String): A string of HTML to create on the fly. 

例子:

jQuery Code

$("p").add("<span>Again</span>")

Before

<p>Hello</p>

Result:

[ <p>Hello</p>, <span>Again</span> ]


函数:add(elements)

功能:给匹配元素添加dom元素

返回:jQuery对象

参数:dom元素或数组

例子:

jQuery Code

$("p").add( document.getElementById("a") )

Before

<p>Hello</p><p><span id="a">Hello Again</span></p>

Result:

[ <p>Hello</p>, <span id="a">Hello Again</span> ]



jQuery Code

$("p").add( document.forms[0].elements )

Before

<p>Hello</p><p><form><input/><button/></form>

Result:

[ <p>Hello</p>, <input/>, <button/> ]


函数:children(expr)

功能:查找子节点

返回:jQuery对象

参数:查找表达式

例子:

jQuery Code

$("div").children()

Before

<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>

Result:

[ <span>Hello Again</span> ]



jQuery Code

$("div").children(".selected")

Before

<div><span>Hello</span><p class="selected">Hello Again</p><p>And Again</p></div>

Result:

[ <p class="selected">Hello Again</p> ]
函数:contains(str)

功能:查找匹配元素中包含str字符的节点

返回:jQuery对象

参数:要查找的string

例子:

jQuery Code

$("p").contains("test")

Before

<p>This is just a test.</p><p>So is this</p>

Result:

[ <p>This is just a test.</p> ]
 
函数:end()

功能:返回到最开始的匹配元素

返回:jQuery对象

参数:

例子:

jQuery Code

$("p").find("span").end();

Before

<p><span>Hello</span>, how are you?</p>

Result:

[ <p></p> ]

函数:filter(expression)

功能:返回满足过滤条件表达式的元素

返回:jQuery对象

参数:expression (String): Expression(s) to search with. 

例子:

Selects all paragraphs and removes those without a class "selected".

jQuery Code

$("p").filter(".selected")

Before

<p class="selected">Hello</p><p>How are you?</p>

Result:

[ <p class="selected">Hello</p> ]



Selects all paragraphs and removes those without class "selected" and being the first one.

jQuery Code

$("p").filter(".selected, :first")

Before

<p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>

Result:

[ <p>Hello</p>, <p class="selected">And Again</p> ]

函数:filter(filter)

功能:返回满足过滤函数的元素

返回:jQuery对象

参数:

例子:

Remove all elements that have a child ol element



jQuery Code

$("p").filter(function(index) {

  return $("ol", this).length == 0;

})

Before

<p><ol><li>Hello</li></ol></p><p>How are you?</p>

Result:

[ <p>How are you?</p> ]

函数:find(expr)

功能:

例子:

Starts with all paragraphs and searches for descendant span elements, same as $("p span") 



jQuery Code

$("p").find("span");

Before

<p><span>Hello</span>, how are you?</p>

Result:

[ <span>Hello</span>

函数:is(expr)

功能:判断当前元素是否符合expr表达式

例子:

Returns true, because the parent of the input is a form element



jQuery Code

$("input[@type='checkbox']").parent().is("form")

Before

<form><input type="checkbox" /></form>

Result:

true



Returns false, because the parent of the input is a p element



jQuery Code

$("input[@type='checkbox']").parent().is("form")

Before

<form><p><input type="checkbox" /></p></form>

Result:

false

[code]函数:next(expr)
功能:返回匹配元素之后的兄弟节点
返回:jQuery对象
参数:expr (String): (optional) An expression to filter the next Elements with 
例子:
Find the very next sibling of each paragraph.

jQuery Code
$("p").next()
Before
<p>Hello</p><p>Hello Again</p><div><span>And Again</span></div>
Result:
[ <p>Hello Ag
分享到:
评论

相关推荐

    jQuery操作DOM解析

    **jQuery操作DOM解析** 在Web开发中,DOM(Document Object Model)是HTML或XML文档的结构化表示,它允许程序和脚本动态更新、添加、删除和改变元素。jQuery库简化了JavaScript对DOM的操作,提供了丰富的API来处理...

    JQuery:常用方法一览

    - **功能**:创建一个jQuery对象,包含DOM树中的元素。 - **示例**:`var $elems = $("p");` 3. **`$(function(){...})`**: - **功能**:文档加载完成时执行函数。 - **示例**:`$(function() { $("p").hide()...

    jquery 常用 Dom操作

    ### jQuery 常用 DOM 操作详解 #### 属性(Attribute) 在 jQuery 中,属性操作主要包括获取、设置或修改元素的属性值。 1. **添加类(`addClass()`)** - **语法**:`$("selector").addClass("classname");` -...

    jquery15.rar

    jQuery是一个广泛使用的JavaScript库,它极大地简化了JavaScript的DOM操作、事件处理、动画制作以及Ajax交互。本教程针对初学者,旨在帮助您在短短15天内掌握jQuery的核心概念和技术,实现高效网页开发。 第1天:...

    JQUERY技术内幕:深入解析QUERY架构设计与实现原理 完整版 共两个包

    深入解析jquery架构设计与实现原理》首先通过...最后详细分析了功能模块的源码实现,包括:属性操作attributes、事件系统events、dom遍历traversing、dom操作manipulation、样式操作css、异步请求ajax、动画effects

    jquery入门(2012年版)

    Chapter 3: Traversing the DOM 29 Chapter 4: DOM Manipulation with jQuery 43 Chapter 5: An Introduction to Events59 Chapter 6: More Events 71 Chapter 7: Animation 83 Chapter 8: Ajax with jQuery ...

    jquery:jquery源码分析

    jQuery 项目源码主要分为几个部分:core(核心)、selector(选择器引擎)、traversing(遍历)、manipulation(DOM 操作)、event(事件)、ajax(AJAX)等。随着版本的更新,jQuery 不断优化性能,增强功能,同时...

    jQuery:jQuery 源码分析

    jQuery 是一个广泛使用的 JavaScript 库,它极大地简化了 DOM 操作、事件处理、动画效果以及 AJAX 交互。jQuery 的流行在于其简洁的 API 和高效的代码实现,使得开发者能够用更少的代码实现更多的功能。 **一、...

    jquery:jquery原始码学习

    1. 链式调用:jQuery的标志性特性之一就是链式调用,它允许开发者在单个对象上调用多个方法,极大地提高了代码的可读性和效率。这种设计是通过在每个方法返回自身实例实现的。 2. 选择器:jQuery对CSS选择器的封装...

    jQuery类库文档 包括 api, js

    - **DOM操作(Traversing)**:jQuery提供了一系列方法来遍历DOM树,如`children()`, `parent()`, `siblings()` 和 `find()` 等,它们简化了元素间的导航。 - **属性操作(Attributes)**:`attr()` 方法用于获取或...

    jQuery的使用方法例子

    以上只是jQuery核心API的一部分,实际使用中还有更多的方法和功能,如AJAX的全局事件、插件机制等。通过熟练掌握这些基础API,开发者可以轻松实现网页的动态交互,提高开发效率。在实际项目中,配合详细的文档和示例...

    jQuery技术内幕 深入解析jQuery架构设计与实现原理

    最后详细分析了功能模块的源码实现,包括:属性操作attributes、事件系统events、dom遍历traversing、dom操作manipulation、样式操作css、异步请求ajax、动画effects。  《jquery技术内幕:深入解析jquery架构设计...

    JQuery In Action.PDF

    - **Making DOM Elements:** Creating new DOM elements is made simple with JQuery. The `$("&lt;element&gt;")` function allows developers to generate new elements dynamically. - **Example:** `var newDiv = $...

    jquery技术内幕:深入解析jquery架构设计与实现原理 完整版第二个包

    深入解析jquery架构设计与实现原理》首先通过...最后详细分析了功能模块的源码实现,包括:属性操作attributes、事件系统events、dom遍历traversing、dom操作manipulation、样式操作css、异步请求ajax、动画effects

    jQuery常用方法jQuery常用方法

    这部分介绍如何使用jQuery处理常见的DOM事件。 - **jQueryEvent: ready(fn)**: 当DOM准备好时执行函数。 - **bind(type[, data], fn)**: 给匹配元素绑定一个事件处理程序。支持多种事件类型如:`click`, `blur`, `...

    JQuery_Api文档

    2. **DOM操作(Traversing)**:JQuery提供了丰富的DOM操作方法,如`append()`用于在元素内部添加内容,`prepend()`在元素内部前面插入内容,`after()`和`before()`则分别在元素后面和前面插入新元素。 3. **事件...

    Jquery1.8 chm 帮助文档

    jQuery的核心API包括选择器(Selectors)、DOM操作(Traversing)、事件(Events)、动画(Effects)和Ajax(Ajax)等模块。在jQuery 1.8中,选择器引擎进行了优化,提高了性能;DOM操作方法如`append()`、`prepend...

    jquery api网页版(离线)

    2. **DOM操作(Traversing)**:jQuery提供了丰富的DOM遍历方法,如`children()`,`parent()`,`siblings()`等,用于查找、添加或删除DOM元素。 3. **属性操作(Attributes)**:可以使用`attr()`和`removeAttr()`...

    jQuery API 官方文档 最新版下载

    jQuery 是一个广泛使用的JavaScript库,它极大地简化了JavaScript的DOM操作、事件处理、动画制作以及Ajax交互。jQuery API(应用程序接口)是开发者使用jQuery库进行网页开发的重要参考资源,提供了详尽的函数、方法...

Global site tag (gtag.js) - Google Analytics