`

Jquery The $() factory function

阅读更多
jQuery	Description
Tag Name:	Represents a tag name available in the DOM. For example $('p') selects all paragraphs in the document.
Tag ID:	Represents a tag available with the given ID in the DOM. For example $('#some-id') selects the single element in the document that has an ID of some-id.
Tag Class:	Represents a tag available with the given class in the DOM. For example $('.some-class') selects all elements in the document that have a class of some-class.
All the above items can be used either on their own or in combination with other selectors. All the jQuery selectors are based on the same principle except some tweaking.

NOTE: The factory function $() is a synonym of jQuery() function. So in case you are using any other JavaScript library where $ sign is conflicting with some thing else then you can replace $ sign by jQuery name and you can use function jQuery() instead of $().

Example:

Following is a simple example which makes use of Tag Selector. This would select all the elements with a tag name p.

<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() {
      var pars = $("p");
      for( i=0; i<pars.length; i++ ){
         alert("Found paragraph: " + pars[i].innerHTML);
      }
   });
   </script>
</head>
<body>
   <div>
      <p class="myclass">This is a paragraph.</p>
      <p id="myid">This is second paragraph.</p>
      <p>This is third paragraph.</p>
   </div>
</body>
</html>
To understand it in better way you can Try it yourself.

How to use Selectors?
The selectors are very useful and would be required at every step while using jQuery. They get the exact element that you want from your HTML document.

Following table lists down few basic selectors and explains them with examples.

Selector	Description
Name	Selects all elements which match with the given element Name.
#ID	Selects a single element which matches with the given ID
.Class	Selects all elements which match with the given Class.
Universal (*)	Selects all elements available in a DOM.
Multiple Elements E, F, G	Selects the combined results of all the specified selectors E, F or G.
Similar to above syntax and examples, following examples would give you understanding on using different type of other useful selectors:

$('*'): This selector selects all elements in the document.

$("p > *"): This selector selects all elements that are children of a paragraph element.

$("#specialID"): This selector function gets the element with id="specialID".

$(".specialClass"): This selector gets all the elements that have the class of specialClass.

$("li:not(.myclass)"): Selects all elements matched by <li> that do not have class="myclass".

$("a#specialID.specialClass"): This selector matches links with an id of specialID and a class of specialClass.

$("p a.specialClass"): This selector matches links with a class of specialClass declared within <p> elements.

$("ul li:first"): This selector gets only the first <li> element of the <ul>.

$("#container p"): Selects all elements matched by <p> that are descendants of an element that has an id of container.

$("li > ul"): Selects all elements matched by <ul> that are children of an element matched by <li>

$("strong + em"): Selects all elements matched by <em> that immediately follow a sibling element matched by <strong>.

$("p ~ ul"): Selects all elements matched by <ul> that follow a sibling element matched by <p>.

$("code, em, strong"): Selects all elements matched by <code> or <em> or <strong>.

$("p strong, .myclass"): Selects all elements matched by <strong> that are descendants of an element matched by <p> as well as all elements that have a class of myclass.

$(":empty"): Selects all elements that have no children.

$("p:empty"): Selects all elements matched by <p> that have no children.

$("div[p]"): Selects all elements matched by <div> that contain an element matched by <p>.

$("p[.myclass]"): Selects all elements matched by <p> that contain an element with a class of myclass.

$("a[@rel]"): Selects all elements matched by <a> that have a rel attribute.

$("input[@name=myname]"): Selects all elements matched by <input> that have a name value exactly equal to myname.

$("input[@name^=myname]"): Selects all elements matched by <input> that have a name value beginning with myname.

$("a[@rel$=self]"): Selects all elements matched by <p> that have a class value ending with bar

$("a[@href*=domain.com]"): Selects all elements matched by <a> that have an href value containing domain.com.

$("li:even"): Selects all elements matched by <li> that have an even index value.

$("tr:odd"): Selects all elements matched by <tr> that have an odd index value.

$("li:first"): Selects the first <li> element.

$("li:last"): Selects the last <li> element.

$("li:visible"): Selects all elements matched by <li> that are visible.

$("li:hidden"): Selects all elements matched by <li> that are hidden.

$(":radio"): Selects all radio buttons in the form.

$(":checked"): Selects all checked boxex in the form.

$(":input"): Selects only form elements (input, select, textarea, button).

$(":text"): Selects only text elements (input[type=text]).

$("li:eq(2)"): Selects the third <li> element

$("li:eq(4)"): Selects the fifth <li> element

$("li:lt(2)"): Selects all elements matched by <li> element before the third one; in other words, the first two <li> elements.

$("p:lt(3)"): selects all elements matched by <p> elements before the fourth one; in other words the first three <p> elements.

$("li:gt(1)"): Selects all elements matched by <li> after the second one.

$("p:gt(2)"): Selects all elements matched by <p> after the third one.

$("div/p"): Selects all elements matched by <p> that are children of an element matched by <div>.

$("div//code"): Selects all elements matched by <code>that are descendants of an element matched by <div>.

$("//p//a"): Selects all elements matched by <a> that are descendants of an element matched by <p>

$("li:first-child"): Selects all elements matched by <li> that are the first child of their parent.

$("li:last-child"): Selects all elements matched by <li> that are the last child of their parent.

$(":parent"): Selects all elements that are the parent of another element, including text.

$("li:contains(second)"): Selects all elements matched by <li> that contain the text second.

You can use all the above selectors with any HTML/XML element in generic way. For example if selector $("li:first") works for <li> element then $("p:first") would also work for <p> element.
分享到:
评论

相关推荐

    jQuery validata插件实现方法

    factory(root.jQuery,plug) })(window,function($,plug){ /* 在这里写逻辑 一:默认的参数 var __dEFAULTS__, 二:规则(可根据业务需求自己配置) var __RULES__, 三:原型 var __PROTOTYPE__,

    jquery Uploadify

    **jQuery Uploadify** 是一个基于jQuery的文件上传插件,它允许用户在Web应用程序中实现异步、批量或单个文件上传功能。该插件以其易用性、自定义性和高性能而受到开发者们的欢迎。在描述中提到,可能有些初学者在...

    custom-jquerymobile-widget:自定义jquerymobile小部件

    本文将深入探讨如何基于jQuery UI Widget Factory来构建自定义的jQuery Mobile小部件,并介绍相关的JavaScript技术。 首先,jQuery UI Widget Factory 是jQuery的一个核心部分,它提供了一种标准化的方法来创建可...

    部分jQuery插件

    6. **Widget Factory(小部件工厂)**:jQuery UI的基础框架,使得创建新的自定义组件变得简单。 **使用jQuery UI** 在项目中使用jQuery UI,首先需要引入jQuery库和jQuery UI的CSS及JS文件。然后,通过简单的...

    js表单源码 jquery.form.rar

    (function (factory) { "use strict"; if (typeof define === 'function' && define.amd) { // using AMD; register as anon module define(['jquery'], factory); } else { // no AMD; invoke directly ...

    jquerylayout

    - **Widget Factory**: `jQuery UI` 使用Widget Factory来创建可复用的组件,`jQuery Layout` 可以利用此工厂实现更丰富的用户交互和视觉效果。 - **ThemeRoller**: 结合 `jQuery UI` 的ThemeRoller工具,可以为 `...

    jquery 1.5 api 参考手册

    1. **Widget Factory**: 1.5.1版本引入了Widget Factory,为创建可重用、可配置的UI组件提供了一种标准化的方法。 2. **对话框(Dialog)**: `.dialog()`方法创建可交互的弹出窗口,常用于警告、确认或输入信息。 ...

    脚本框架jquery

    - **事件处理(Events)**:jQuery 封装了JavaScript的事件处理,使得绑定和解绑事件更为方便,如 `$(selector).click(function() {...})` 和 `$(selector).off('click')`。 - **动画效果(Effects)**:jQuery ...

    JQuery插件开发 + 插件

    3. Plugin Factory模式:用于生成更复杂、可配置的插件。 四、`clip-图片剪裁`相关知识点 在压缩包中的“clip-图片剪裁”很可能是一个用于图像裁剪的jQuery插件。它可能提供了以下功能: 1. 图像预览:在用户选择...

    Jquery Uploadify +Java 文件上传

    `jQuery Uploadify` 和 `Java` 的结合为开发者提供了一个高效且用户友好的文件上传解决方案。这篇内容将深入探讨如何利用这两个技术实现多文件上传,并与`commons-fileupload`库结合提升上传性能。 `jQuery ...

    使用commons-fileupload-1.2.2和jquery的progressbar实现的进度条

    本篇将讨论如何使用Apache Commons FileUpload 1.2.2库和jQuery的progressbar插件来实现这样的功能。 Apache Commons FileUpload是一个Java库,专门处理HTTP请求中的多部分形式数据,即通常用于上传文件的数据格式...

    jquery美化滚动条

    自定义美化滚动条,(function(factory){ if(typeof module!=="undefined" && module.exports){ module.exports=factory; }else{ factory(jQuery,window,document); } }

    jqueryui入门

    同时,jQuery UI 提供了一系列工具,如 Widget Factory,帮助开发者构建可复用的 UI 组件。 总的来说,jQuery UI 是一个强大且灵活的前端框架,它极大地简化了网页交互设计。无论你是初学者还是经验丰富的开发者,...

    ssh2+ajax+jquery+json验证用户名是否存在

    在这个场景中,前端使用jQuery库,一个流行的JavaScript库,来简化AJAX请求的编写。jQuery提供了方便的API,可以轻松地发起异步请求,获取服务器反馈的数据。JSON(JavaScript Object Notation)则作为数据交换格式...

    写jQuery插件时的注意点

    module.exports = factory(require('jquery')); } else { // 浏览器全局 root.returnExports = factory(root.jQuery); } })(this, function($) { // 插件实现 }); ``` 7. **避免改变原始元素集**:在遍历...

    jQueryWidgetFactory1.8.21开发指南.doc

    ### jQuery Widget Factory 1.8.21 开发指南 #### 概述 jQuery Widget Factory 是 jQuery UI 提供的一个强大的工具包,它简化了自定义控件的开发过程。通过这个框架,开发者可以轻松地创建出功能丰富且高度可定制...

    jQuery Uploadify + Apache Fileupload异步上传文件示例

    'onUploadSuccess': function(file, data, response) { // 处理成功回调 } }); ``` - 后端Java部分: ```java // 在Servlet或Controller中 DiskFileItemFactory factory = new DiskFileItemFactory(); ...

    jQuery UI Grid 模态框中的表格实例代码

    app.factory('modal', ['$compile', '$rootScope', function ($compile, $rootScope) { // modal factory 的实现 }]); ``` 在这个例子中,我们还创建了一个名为`modal`的工厂服务,用于管理Bootstrap模态框的开闭...

    使用JQuery实现的分页插件分享

    (function (factory) { if (typeof define === "function" && define.amd) { // AMD模式 define(["jquery"], factory); } else { // 全局模式 factory(jQuery); } })(function ($) { // 插件代码... }); ```...

    greetr:jQuery原始代码分析,并打造了一个简单的js类库

    ,写一下关于jQuery部分的笔记greetr是仿照jQuery的整体结构写的一个小的函数库Greetr ,代码在Greetr文件夹中jQuery的版本v3.5.1IIEF(立即调用函数表达式) ( function ( global , factory ) {} ) ( typeof window...

Global site tag (gtag.js) - Google Analytics