jQuery核心之jQuery Object及其相关的常用方法
1、jQuery Object 和 原生的DOM对象之间有许多方法是不一样的,用jQuery的方法大部分情况下返回的是jQuery Object,但是jQuery也提供了一些方法可以很轻松的获取原生的DOM对象。
// Selecting only the first <h1> element on the page (in a jQuery object)var headings = $( "h1" );var firstHeading = headings.eq( 0 );
// Selecting only the first <h1> element on the page.var firstHeadingElem = $( "h1" ).get( 0 );
// Selecting only the first <h1> element on the page (alternate approach).var firstHeadingElem = $( "h1" )[ 0 ];
// 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"
<div class="grandparent"><div class="parent"><div class="child"><span class="subchild"></span></div></div><div class="surrogateParent1"></div><div class="surrogateParent2"></div></div>
// 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" );
// 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" );
// 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();
// Working with classes.var h1 = $( "h1" );h1.addClass( "big" );h1.removeClass( "big" );h1.toggleClass( "big" );if ( h1.hasClass( "big" ) ) {...}
// 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();
// 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 datavar firstLi = $( "#myList li:first" );firstLi.data( "contentDiv" ).html( "new content" );
jQuery核心之jQuery Object及其相关的常用方法的更多相关文章
- Jquery核心函数
在Jquery中,所有的DOM对象都将封装成Jquery对象,而且只有Jquery对象才能使用Jquery方法或者属性来执行相应的操作. 所以Jquery提供了一个可以将DOM对象封装成Jquery对 ...
- jQuery核心函数和静态方法
jQuery核心函数 从jQuery文档中可以看出,jQuery核心函数一共3大类4小类 jQuery(callback) 当DOM加载完成后执行传入的回调函数 <script> $(fu ...
- 浅谈 jQuery 核心架构设计
jQuery对于大家而言并不陌生,因此关于它是什么以及它的作用,在这里我就不多言了,而本篇文章的目的是想通过对源码简单的分析来讨论 jQuery 的核心架构设计,以及jQuery 是如何利用javas ...
- 谈一谈jQuery核心架构设计(转)
jQuery对于大家而言并不陌生,因此关于它是什么以及它的作用,在这里我就不多言了,而本篇文章的目的是想通过对源码简单的分析来讨论 jQuery 的核心架构设计,以及jQuery 是如何利用javas ...
- Jq_DOM元素方法跟JQuery 核心函数跟JQuery 事件方法
JQuery DOM 元素 函数 描述 .get() 从队列中删除所有未运行的项目. .ind ...
- 13.11.20 jquery 核心 siblings() 获得同类(不包含自己)循环所有,
jquery 核心1.选择器,2. 创建dom 元素 3. jquery 执行时 4. 延迟执行 5. 循环 6. 计算长度.7.8 获得选择器和所在节点 9. 获得下标 10. 元素存放数据 11 ...
- jQuery笔记: 基本概念与jQuery核心
目录 初识jQuery 为什么要使用jQuery? 如何使用jQuery? jQuery与js加载模式不同 jQuery入口函数的四种写法 jQuery的访问符冲突问题 jQuery核心函数和jQue ...
- jquery 核心
1.jquery核心函数 1.1 jQuery([selector,[context]]); $("#id"),$(document.body),$(" ...
- JQuery --- 第一期 (初识jQuery, JQuery核心函数和工具方法)
个人学习笔记 初识jQuery 1.我的第一个JQuery <!DOCTYPE html> <html lang="en"> <head> & ...
随机推荐
- 使用Visual Studio下自带的SQL Server Express
软件环境:Windows7(x64) + Visual Studio 2010 + SQL Server Express 2008 1.配置数据库 装VS2010不小心把自带的SQL Server 2 ...
- 【转载】James Whittaker:经营成功的测试职业生涯
转注:这篇文章出自 James A. Whittaker ,但未找到原始出处/译者.如果有知道原始出处的朋友,可在评论这留言. 你是如何开始做测试工作的? 1989年,我在田纳西大学读研究生的时候, ...
- Apache模块mod_security 和 Nginx过滤配置
1.安装mod_securityyum install mod_security 2.安装mod_security_crsyum install mod_security_crs 3.在/etc/ht ...
- Thinking in Java——笔记(10)
Inner Classes It allows you to group classes that logically belong together and to control the visib ...
- hadoop2.5重新编译问题
这几天一直在搭建hadoop环境,由于2.5以及2.6的版本需要在64位环境下重新编译,所以中间走了不少弯路.现在总结一下,由于手头资源紧张,只能在pc上模拟环境,具体环境如下: 宿主机:联想的笔记本 ...
- 使用Notepad++代替笨拙的Arduino IDE
Arduino自带的IDE 太不好用了,下面是用强大,轻量级,免费,开源,丰富插件的的Notepad++编辑器来打造Arduino开发环境 . 配置过程对新生来说可能有些繁琐,但是我尽力写的很详细了, ...
- Json 、 Jsonp
SONP is simply a hack to allow web apps to retrieve data across domains. It could be said that it vi ...
- C++ Primer Pluse_8_课后题
#include <iostream> #include <string> #include<cstring> using namespace std; void ...
- appium 等待页面元素加载
前面没找到合适的函数,用的是 try{Thread.sleep(10);}catch(Exception e){} 但是这个时间得自己控制,强制等待加载,很2的办法,后来终于找到一个合适的替代函数了 ...
- UE4中使用数据表(Data Table)
本文依据官方文档数据驱动游戏性元素整理而来. 做过游戏的应该都清楚,如果游戏稍微有点规模,那么使用数据驱动来做游戏一般是必不可少的一步,一般也就是策划通过本表的方式来解决.下面我们来简单说一下UE4中 ...