jQuery Selectors Overview

Table of Contents

jQuery selectors are used to select DOM elements in the HTML page. Most jQuery code starts with a jQuery selector. The code selects one or more HTML elements and then traverse the DOM elements using the jQuery traversal features, manipulate the DOM elements via the jQuery DOM manipulation features, add event listeners to them via the jQuery event features, or add effects to them via the jQuery effects features.

jQuery contains a wide range of selectors which can select HTML elements based on their element name, id, attribute values, visibility, element order and many other criteria. This jQuery selector tutorial will cover the most commonly used selectors. For a full list of all jQuery selectors, see the following page in the official jQuery documentation:

http://api.jquery.com/category/selectors/

jQuery Selector Overview

jQuery selector is a string which specifies which HTML elements to select. The selector string is passed to the$() or jQuery() selection function which returns a collection of the selected elements.

The jQuery selector syntax is the same as CSS selectors, so if you are familiar with CSS selectors you will learn jQuery selectors very quickly.

jQuery lets you select elements based on the following criteria:

  • Element name (e.g. ‘p‘, ‘a‘, ‘div‘ etc.)
  • Element id
  • Element CSS class
  • Element attributes
  • Element visibility
  • Element order
  • Form Fields
  • Element parents or children
  • Combinations of the above

I will cover each of these options in the sections below.

jQuery Selector Example

To get you started, let me show you a simple jQuery selector example:

var theDiv = $("#theDiv");

This example selects the div element with the id theDiv, meaning the div element which has an id attribute with the value theDiv.

The $() and jQuery Functions

The $() returns a jQuery enhanced set of elements. This jQuery enhanced set of elements contains a lot of utility functions which can be applied to all elements in the set. For instance, here is how you set a CSS class on all the selected elements in the set:

var theDiv = $("#theDiv");

theDiv.addClass("newCssClass");

This example sets the CSS class newCssClass on all elements in the set. Since the example only selects a single element, only one element will get the new CSS class.

A shorter way of writing the above code is:

$("#theDiv").addClass("newCssClass");

Notice how the addClass() function is chained directly to the $() call.

Many of the utility functions on the jQuery enhanced selection set return the selection set itself. That means that you can chain method calls on the selection set, like this:

$("#theDiv").addClass("newCssClass")
            .css("border", "1px solid #cccccc")
            .css("background-color", "#f0f0f0")
    ;

As hinted, this technique is called method chaining. In the JavaScript world an API designed to work with method chaining is also referred to as a "fluent" API. jQuery‘s fluent style API makes it very easy to make changes to the DOM.

Instead of the $() function you can also use the jQuery() function. The two functions do exactly the same thing, but sometimes you might be using other JavaScript libraries which also define a $() function. In that case, use thejQuery() function instead.

The many utility functions on the selection set are covered in separate texts in this jQuery tutorial trail. For instance, there are traversal functions, CSS manipulations functions, DOM manipulation functions, various effects (like fading and scrolling of elements), event listener functions, and much more.

时间: 2024-10-13 15:27:06

jQuery Selectors Overview的相关文章

JQuery Selectors(选择器) 方法说明

JQuery Selectors 方法说明 基本选择器 $("#myDiv") 匹配唯一的具有此id值的元素$("div") 匹配指定名称的所有元素$(".myClass") 匹配具有此class样式值的所有元素$("*") 匹配所有元素$("div,span,p.myClass") 联合所有匹配的选择器 层叠选择器 $("form input") 后代选择器,选择ancestor的所有

JQuery Selectors 方法说明

基本选择器 $("#myDiv") 匹配唯一的具有此id值的元素 $("div") 匹配指定名称的所有元素 $(".myClass") 匹配具有此class样式值的所有元素 $("*") 匹配所有元素 $("div,span,p.myClass") 联合所有匹配的选择器 层叠选择器 $("form input") 后代选择器,选择ancestor的所有子孙节点 $("#main

jQuery源码

/*! * jQuery JavaScript Library v1.8.3 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2012 jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: Tue Nov 13 20

JQuery日记 6.3 JQuery遍历模块

jQuery.extend({ // 返回elem延DOM树某个方向访问的所有节点,直到遇到until条件 dir: function( elem, dir, until ) { var matched = [], truncate = until !== undefined; while ( (elem = elem[ dir ]) && elem.nodeType !== 9 ) { if ( elem.nodeType === 1 ) { if ( truncate &&am

jquery 2.1.0 源码

/*! * jQuery JavaScript Library v2.1.0 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2

jQuery常用方法一览

Attribute: $("p").addClass(css中定义的样式类型); 给某个元素添加样式 $("img").attr({src:"test.jpg",alt:"test Image"}); 给某个元素添加属性/值,参数是map $("img").attr("src","test.jpg"); 给某个元素添加属性/值 $("img").a

jQuery 事件方法大全-超全的总结

jquery常用的事件: /* on off hover blur change click dblclick focus keydown keyup keypress mousedown resize scroll select submit */ 元素事件列表说明: 注:不带参数的函数,其参数为可选的 fn.jQuery不支持form元素的reset事件. 事件 描述 支持元素或对象 blur( ) 元素失去焦点 a, input, textarea, button, select, lab

JQuery :Not() Selector Example

It’s been a while since I wrote about JQuery. I am spending most of my time these days on backend technologies. Recently while working on a typical requirement on UI, I had to play with some tricky JQuery selectors. Typically, most of the times we us

很不错的jQuery学习资料和实例

这些都是学习Jquery很不错的资料,整理了一下,分享给大家. 希望能对大家的学习有帮助. 帕兰 Noupe带来的51个最佳jQuery教程和实例, 向大家介绍了jQuery的一些基本概念和使用的相关教程,  如果你对jQuery感兴趣, 也可以查看帕兰写的文章: 37个更加出色的jQuery插件 45个新鲜出炉的jQuery插件 50多个强大的jQuery插件应用实例 John Resig John Resig, 这位是 JQuery JavaScript脚本库的创建者, 同时也是Mozill