1、jQuery Object 和 原生的DOM对象之间有许多方法是不一样的,用jQuery的方法大部分情况下返回的是jQuery Object,但是jQuery也提供了一些方法可以很轻松的获取原生的DOM对象。


先来看:获取jQuery Object之一,
// Selecting only the first <h1> element on the page (in a jQuery object)
var headings = $( "h1" );
var firstHeading = headings.eq( 0 );
这里主要是因为headings里面包含有多个jQuery Object,所以用这个方法来获取特定的jQuery Object。

获取DOM 对象:
法1、
// Selecting only the first <h1> element on the page.
var firstHeadingElem = $( "h1" ).get( 0 );

法2、
// Selecting only the first <h1> element on the page (alternate approach).
var firstHeadingElem = $( "h1" )[ 0 ];
通过这两种方法获取的对象是原生DOM 对象,所以它有innerHTML,appendChild,等原生的js方法,但是没有jQuery特有的html(),after()等方法

还得注意:每一个通过选择器获取的jQuery Object是独一无二的,也就是它们之间的===关系总是返回false.
alert( $( "#logo" ) === $( "#logo" ) ); // alerts "false"

但是:用get()方法却可以获取同一个DOM 元素 ,此时它们之间的===关系返回true.
alert( $( "#logo" ).get(0) === $( "#logo" ).get(0) ); 

为了更好的区分jQuery Object和 native DOM Object 可以把$加在jQuery Object面前来标识它,这样使得代码更加容易阅读
// Comparing DOM elements (with more readable variable names).
var $logo1 = $( "#logo" );
var logo1 = $logo1.get( 0 );
var $logo2 = $( "#logo" );
var logo2 = $logo2.get( 0 );
alert( logo1 === logo2 ); // alerts "true" 

2、遍历
<div class="grandparent">
<div class="parent">
<div class="child">
<span class="subchild"></span>
</div>
</div>
<div class="surrogateParent1"></div>
<div class="surrogateParent2"></div>
</div> 
parent方法(获取父级元素):
// Selecting an element's direct parent:
// returns [ div.child ]
$( "span.subchild" ).parent();
// Selecting all the parents of an element that match a given selector:
// returns [ div.parent ]
$( "span.subchild" ).parents( "div.parent" );
// returns [ div.child, div.parent, div.grandparent ]
$( "span.subchild" ).parents();
// Selecting all the parents of an element up to, but *not including* the selector:
// returns [ div.child, div.parent ]
$( "span.subchild" ).parentsUntil( "div.grandparent" );
// Selecting the closest parent, note that only one parent will be selected
// and that the initial element itself is included in the search:
// returns [ div.child ]
$( "span.subchild" ).closest( "div" );
// returns [ div.child ] as the selector is also included in the search:
$( "div.child" ).closest( "div" ); 

children方法(获取子孙元素):
// Selecting an element's direct children:
// returns [ div.parent, div.surrogateParent1, div.surrogateParent2 ]
$( "div.grandparent" ).children( "div" );
// Finding all elements within a selection that match the selector:
// returns [ div.child, div.parent, div.surrogateParent1, div.surrogateParent2 ]
$( "div.grandparent" ).find( "div" ); 
siblings方法(获取同级元素):
注意元素里面的顺序
// Selecting a next sibling of the selectors:
// returns [ div.surrogateParent1 ]
$( "div.parent" ).next();
// Selecting a prev sibling of the selectors:
// returns [] as No sibling exists before div.parent
$( "div.parent" ).prev();
// Selecting all the next siblings of the selector:
// returns [ div.surrogateParent1, div.surrogateParent2 ]
$( "div.parent" ).nextAll();
// returns [ div.surrogateParent1 ]
$( "div.parent" ).nextAll().first();
// returns [ div.surrogateParent2 ]
$( "div.parent" ).nextAll().last();
// Selecting all the previous siblings of the selector:
// returns [ div.surrogateParent1, div.parent ]
$( "div.surrogateParent2" ).prevAll();
// returns [ div.surrogateParent1 ]
$( "div.surrogateParent2" ).prevAll().first();
// returns [ div.parent ]
$( "div.surrogateParent2" ).prevAll().last(); 

获取同级(除自己以外)的所有元素
// Selecting an element's siblings in both directions that matches the given selector:
// returns [ div.surrogateParent1, div.surrogateParent2 ]
$( "div.parent" ).siblings();
// returns [ div.parent, div.surrogateParent2 ]
$( "div.surrogateParent1" ).siblings(); 

3、CSS样式及其尺寸:
还有设置css的方法:
$("h1").css("color":"ffffff");
$("h1").css(
    {
        "background-color":"#6699cc";
    }
)
等。
增加删除CSS样式类:
// Working with classes.
var h1 = $( "h1" );
h1.addClass( "big" );
h1.removeClass( "big" );
h1.toggleClass( "big" );
if ( h1.hasClass( "big" ) ) {
...
}
addClass() 增加样式类
removeClass() 删除样式类
toggleClass("a") 如果样式类a存在,那么就删除样式类a,如果它不存在,那么就增加样式类a(起到一个样式切换的效果)

尺寸:
// Basic dimensions methods.
// Sets the width of all <h1> elements.
$( "h1" ).width( "50px" );
// Gets the width of the first <h1> element.
$( "h1" ).width();
// Sets the height of all <h1> elements.
$( "h1" ).height( "50px" );
// Gets the height of the first <h1> element.
$( "h1" ).height();
// Returns an object containing position information for
// the first <h1> relative to its "offset (positioned) parent".
$( "h1" ).position(); 

4、数据方法(Data methods)
// Storing and retrieving data related to an element.
$( "#myDiv" ).data( "keyName", { foo: "bar" } );
$( "#myDiv" ).data( "keyName" ); // Returns { foo: "bar" }


// Storing a relationship between elements using .data()
$( "#myList li" ).each(function() {
var li = $( this );
var div = li.find( "div.content" );
li.data( "contentDiv", div );
});
// Later, we don't have to find the div again;
// we can just read it from the list item's data
var firstLi = $( "#myList li:first" );
firstLi.data( "contentDiv" ).html( "new content" );






jQuery核心之jQuery Object及其相关的常用方法的更多相关文章

  1. Jquery核心函数

    在Jquery中,所有的DOM对象都将封装成Jquery对象,而且只有Jquery对象才能使用Jquery方法或者属性来执行相应的操作. 所以Jquery提供了一个可以将DOM对象封装成Jquery对 ...

  2. jQuery核心函数和静态方法

    jQuery核心函数 从jQuery文档中可以看出,jQuery核心函数一共3大类4小类 jQuery(callback) 当DOM加载完成后执行传入的回调函数 <script> $(fu ...

  3. 浅谈 jQuery 核心架构设计

    jQuery对于大家而言并不陌生,因此关于它是什么以及它的作用,在这里我就不多言了,而本篇文章的目的是想通过对源码简单的分析来讨论 jQuery 的核心架构设计,以及jQuery 是如何利用javas ...

  4. 谈一谈jQuery核心架构设计(转)

    jQuery对于大家而言并不陌生,因此关于它是什么以及它的作用,在这里我就不多言了,而本篇文章的目的是想通过对源码简单的分析来讨论 jQuery 的核心架构设计,以及jQuery 是如何利用javas ...

  5. Jq_DOM元素方法跟JQuery 核心函数跟JQuery 事件方法

    JQuery DOM 元素 函数                            描述 .get()                           从队列中删除所有未运行的项目. .ind ...

  6. 13.11.20 jquery 核心 siblings() 获得同类(不包含自己)循环所有,

    jquery 核心1.选择器,2. 创建dom 元素 3. jquery 执行时 4. 延迟执行 5. 循环 6. 计算长度.7.8 获得选择器和所在节点 9. 获得下标 10. 元素存放数据  11 ...

  7. jQuery笔记: 基本概念与jQuery核心

    目录 初识jQuery 为什么要使用jQuery? 如何使用jQuery? jQuery与js加载模式不同 jQuery入口函数的四种写法 jQuery的访问符冲突问题 jQuery核心函数和jQue ...

  8. jquery 核心

    1.jquery核心函数    1.1 jQuery([selector,[context]]);        $("#id"),$(document.body),$(" ...

  9. JQuery --- 第一期 (初识jQuery, JQuery核心函数和工具方法)

    个人学习笔记  初识jQuery 1.我的第一个JQuery <!DOCTYPE html> <html lang="en"> <head> & ...

随机推荐

  1. VS2010 更改C#类库模板

    首先你找到你自己的 VS2010安装目录,例如我本地是: D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTem ...

  2. 浏览器中Javascript单线程分析

    线程这个特性对于一门语言环境来说是尤其重要的,在Java/C++环境下都提供了多线程API操作. 但在Javascript中据说代码执行时单线程的,大量计算的逻辑会阻塞浏览器HTML渲染,但setTi ...

  3. DBXJSON和ADO的效率真的好低....

    项目需要写了一个JSON和DataSet互转的单元.....支持了Delphi自带的几种DataSet, 结果发现DBXJSON和ADO的效率真的是好低啊........-_-.... 开发环境是XE ...

  4. How to prevent SQL injection attacks?

    In our earlier tutorial on SQL Injection, one way to have prevented the SQL injection attack was by ...

  5. oracle: tochar(sysdate,'D')函数

    学习oracle时碰到tochar(sysdate,'D')函数,但是发现并不是星期几,如今天是20150317,周二,但是得到的值为3 开始以为是系统日期什么的原因,试了试 select to_ch ...

  6. .NET Framework (代码库、通用类型系统CTS、CLR) 简介

    编译C#————>程序集(.exe..dll[MSIL]).元信息[数据信息].可选资源[图片.声音]) |                     | |                   ...

  7. Python开发【第四章】:Python函数剖析

    一.Python函数剖析 1.函数的调用顺序 #!/usr/bin/env python # -*- coding:utf-8 -*- #-Author-Lian #函数错误的调用方式 def fun ...

  8. WEB压力测试工具Pylot试用

    Pylot介绍 转载自[http://www.freehao123.com/pylot-web/] 为了能够准确地评估网站服务器对网络流量的承受能力,我们一般会采取模拟网站用户访问,通过不断地增加并发 ...

  9. 实现easyui datagrid在没有数据时显示相关提示内容

    本示例实现easyui datagrid加载/查询数据时,如果没有相关记录,则在datagrid中显示没有相关记录的提示信息,效果如下图所示 本实例要实现如下图所示的效果: 本示例easyui版本为1 ...

  10. Unity操作

    聚焦到游戏物体: Hierarchy界面选中需要聚焦的物体,双击或者使用快捷键“F”: 在Scene面板中选中物体,使用快捷键“F”   放大缩小物体: alt+鼠标右键:鼠标滑轮   从各个角度观察 ...