`

Jquery Find Elements by index

阅读更多

 

Find Elements by index:

Consider a simple document with the following HTML content:

<html>
<head>
<title>the title</title>
</head>
<body>
   <div>
   <ul>
     <li>list item 1</li>
     <li>list item 2</li>
     <li>list item 3</li>
     <li>list item 4</li>
     <li>list item 5</li>
     <li>list item 6</li>
   </ul>
   </div>
</body>
</html>
  • Above every list has its own index, and can be located directly by using eq(index) method as below example.

  • Every child element starts its index from zero, thus, list item 2 would be accessed by using $("li").eq(1) and so on.

Example:

Following is a simple example which adds the color to second list item.

<html>
<head>
<title>the title</title>
   <script type="text/javascript" 
   src="/jquery/jquery-1.3.2.min.js"></script>
   <script type="text/javascript" language="javascript">
   
   $(document).ready(function() {
      $("li").eq(2).addClass("selected");
   });

   </script>
   <style>
      .selected { color:red; }
  </style>
</head>
<body>
   <div>
   <ul>
     <li>list item 1</li>
     <li>list item 2</li>
     <li>list item 3</li>
     <li>list item 4</li>
     <li>list item 5</li>
     <li>list item 6</li>
   </ul>
   </div>
</body>
</html>

To understand it in better way you can Try it yourself.

Filtering out Elements:

The filter( selector ) method can be used to filter out all elements from the set of matched elements that do not match the specified selector(s). The selector can be written using any selector syntax.

Example:

Following is a simple example which applies color to the lists associated with middle class:

<html>
<head>
<title>the title</title>
   <script type="text/javascript" 
   src="/jquery/jquery-1.3.2.min.js"></script>
   <script type="text/javascript" language="javascript">
   
   $(document).ready(function() {
      $("li").filter(".middle").addClass("selected");
   });

   </script>
   <style>
      .selected { color:red; }
  </style>
</head>
<body>
   <div>
   <ul>
     <li class="top">list item 1</li>
     <li class="top">list item 2</li>
     <li class="middle">list item 3</li>
     <li class="middle">list item 4</li>
     <li class="bottom">list item 5</li>
     <li class="bottom">list item 6</li>
   </ul>
   </div>
</body>
</html>

To understand it in better way you can Try it yourself.

Locating Descendent Elements :

The find( selector ) method can be used to locate all the descendent elements of a particular type of elements. Theselector can be written using any selector syntax.

Example:

Following is an example which selects all the <span> elements available inside different <p> elements:

<html>
<head>
<title>the title</title>
   <script type="text/javascript" 
   src="/jquery/jquery-1.3.2.min.js"></script>
   <script type="text/javascript" language="javascript">

   $(document).ready(function() {
      $("p").find("span").addClass("selected");
   });

   </script>
   <style>
      .selected { color:red; }
  </style>
</head>
<body>
   <p>This is 1st paragraph and <span>THIS IS RED</span></p>
   <p>This is 2nd paragraph and <span>THIS IS ALSO RED</span></p>
</body>
</html>

To understand it in better way you can Try it yourself.

JQuery DOM Traversing Methods:

Following table lists down useful methods which you can use to filter out various elements from a list of DOM elements:

Selector Description
eq( index ) Reduce the set of matched elements to a single element.
filter( selector ) Removes all elements from the set of matched elements that do not match the specified selector(s).
filter( fn ) Removes all elements from the set of matched elements that do not match the specified function.
is( selector ) Checks the current selection against an expression and returns true, if at least one element of the selection fits the given selector.
map( callback ) Translate a set of elements in the jQuery object into another set of values in a jQuery array (which may, or may not contain elements).
not( selector ) Removes elements matching the specified selector from the set of matched elements.
slice( start, [end] ) Selects a subset of the matched elements.

Following table lists down other useful methods which you can use to locate various elements in a DOM:

Selector Description
add( selector ) Adds more elements, matched by the given selector, to the set of matched elements.
andSelf( ) Add the previous selection to the current selection.
children( [selector]) Get a set of elements containing all of the unique immediate children of each of the matched set of elements.
closest( selector ) Get a set of elements containing the closest parent element that matches the specified selector, the starting element included.
contents( ) Find all the child nodes inside the matched elements (including text nodes), or the content document, if the element is an iframe.
end( ) Revert the most recent 'destructive' operation, changing the set of matched elements to its previous state .
find( selector ) Searches for descendent elements that match the specified selectors.
next( [selector] ) Get a set of elements containing the unique next siblings of each of the given set of elements.
nextAll( [selector] ) Find all sibling elements after the current element.
offsetParent( ) Returns a jQuery collection with the positioned parent of the first matched element.
parent( [selector] ) Get the direct parent of an element. If called on a set of elements, parent returns a set of their unique direct parent elements.
parents( [selector] ) Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element).
prev( [selector] ) Get a set of elements containing the unique previous siblings of each of the matched set of elements.
prevAll( [selector] ) Find all sibling elements in front of the current element.
siblings( [selector] ) Get a set of elements containing all of the unique siblings of each of the matched set of elements.
分享到:
评论

相关推荐

    jQuery实现获取元素索引值index的方法

    jQuery作为一个强大的JavaScript库,提供了一系列方法来简化DOM操作,其中“.index()”方法便是用于获取元素索引值的一种有效方式。 首先,我们要了解“.index()”方法的作用,它可以帮助我们获取jQuery对象集合中...

    jQuery find和children方法使用

    在JavaScript的DOM操作中,jQuery库提供了一系列便捷的方法来帮助开发者高效地操作DOM元素,其中`find`和`children`是两个常用的子元素查询方法。本文将详细解析这两个方法的使用,以及它们在实际开发中的应用场景。...

    jQuery中Find选择器用法示例

    本文实例讲述了jQuery中Find选择器用法。分享给大家供大家参考,具体如下: find 可以查找指定节点下的节点,它是会递归查找的,即可以查找子节点的子节点 示例如下: function CalculatePrice() { var totalMoney...

    jQuery中find()方法用法实例

    ### jQuery中find()方法用法实例 #### 知识点概述: jQuery是目前最流行的JavaScript库之一,它极大地简化了JavaScript编程。在jQuery中,`find()`是一个非常有用的方法,用于在匹配元素的后代中查找元素。它的...

    JQuery In Action.PDF

    - **The jQuery Wrapper:** When you use JQuery to select or create elements, you receive a wrapper object that contains all the matched DOM elements. This wrapper provides a consistent interface for ...

    jquery-3.7.0.min.js(jQuery下载)

    jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)...

    jquery.api.3.2.1离线文档手册chm版_by小灰灰整理

    jquery.api.3.2.1离线文档手册chm版_by小灰灰整理

    jQuery.Essentials.

    We start off with a quick glance through the basics of JQuery, followed by the explanation of JQuery selectors, filters, and DOM element manipulation. After this, you will learn how events and ...

    Learning jQuery 3 - Fifth Edition

    Learning jQuery 3 - Fifth Edition by Adam Boduch English | 29 May 2017 | ASIN: B01N2RKEL3 | 448 Pages | AZW3 | 6.45 MB Create efficient and smart web applications with jQuery 3.0 using this step-by-...

    jQuery帮助文档

    jQuery(elements) jQuery(callback) jQuery 对象访问 each(callback) size() length selector context get() get(index) index(subject) 数据缓存 data(name , [value]) removeData(name) queue(name ,...

    jQuery中each()、find()和filter()等节点操作方法详解(推荐)

    使用函数时,它接受一个参数index,这个参数是当前元素在jQuery集合中的索引。在函数内部,你可以使用this关键字来引用当前的DOM元素。例如,如果你想要选择所有带有class="selected"的段落元素,你可以这样做: ``...

    jQuery应用技巧大全modified

    获取jQuery集合的某一项通常通过`eq(index)`或`get(index)`方法。`eq(index)`返回一个新的jQuery对象,`get(index)`返回DOM元素。例如,`$("div").eq(2)`和`$("div").get(2)`分别用于获取索引为2的`&lt;div&gt;`元素,但...

    jQuery 1.5 API 中文版

    $jQuery.pushStack( elements, [name, arguments] ) arr.toArray( ) Interoperability $jQuery.noConflict( [extreme] ) ver. 04-02-2011 Attributes Attributes str.attr( name ) $.attr( name, val ), .attr( map ...

    jquery精简版jquery-small.js

    jquery 精简版 jquery 精简版 jquery 精简版jquery 精简版 jquery 精简版 jquery 精简版 jquery 精简版

    jQuery1.12.4+jQuery中文手册.rar

    **jQuery 1.12.4 知识点详解** jQuery 是一个广泛使用的JavaScript库,它简化了HTML文档遍历、事件处理、动画以及Ajax交互等任务。在本压缩包中,我们有两个版本的jQuery核心库文件:`jquery-1.12.4.js` 和 `jquery...

    jquery高亮图片框 jquery图片展示 jquery效果很好

    标题“jquery高亮图片框 jquery图片展示 jquery效果很好”所指的知识点主要围绕jQuery如何实现高亮图片框以及优雅地展示图片。下面将详细介绍这两个方面,并探讨jQuery库在实现这些功能时的优势。 首先,jQuery高亮...

    jquery mobile index

    jquery mobile index php

    jquery-2.1.4.js 、jquery-2.1.4.min.js 【jquery包 js】

    《jQuery 2.1.4:JavaScript 的强大库与优化版本》 在Web开发领域,jQuery是一个不可或缺的JavaScript库,以其简洁的API和强大的功能深受开发者喜爱。标题中提到的"jquery-2.1.4.js"和"jquery-2.1.4.min.js"正是这...

Global site tag (gtag.js) - Google Analytics