jQuery中的pushStack
2016-01-24 22:27
633 查看
在学习jquery源码的时候,学到了其中的pushStack方法,在这里记录一下
源码为
比如在页面上有两个标签
<div>div</div>
<span>span</span>
这时,这么写
$("div").pushStack($("span"));
此时stack的结构为
由于stack的先入后出的原则,所以$("div").pushStack($("span")).css('background','red');显示为
而jquery的
这个方法可以追溯到上一层,如
jquery中的splice和选择器中的eq等,都使用的是这个原理
源码为
// Take an array of elements and push it onto the stack // (returning the new matched element set) pushStack: function( elems ) { // Build a new jQuery matched element set var ret = jQuery.merge( this.constructor(), elems ); // Add the old object onto the stack (as a reference) ret.prevObject = this; ret.context = this.context; // Return the newly-formed element set return ret; }
比如在页面上有两个标签
<div>div</div>
<span>span</span>
这时,这么写
$("div").pushStack($("span"));
此时stack的结构为
由于stack的先入后出的原则,所以$("div").pushStack($("span")).css('background','red');显示为
而jquery的
这个方法可以追溯到上一层,如
jquery中的splice和选择器中的eq等,都使用的是这个原理
相关文章推荐
- jQuery中的pushStack
- 【jQuery基础学习】00 序
- jquery的一些疑问和解答(待扩展)
- jquery之selectors
- jquery 复制json对象和json对象集合
- Jquery获取父级元素的三种方法
- jquery zTree 查找所有的叶子节点
- jQuery form 表单验证插件(fieldValue)校验表单
- Jquery实现纵向横向菜单
- 用Jquery的attr方法动态添加JSON对象参数
- JavaScript、jQuery与Ajax的关系
- JavaScript jquery及AJAX小结
- jQuery Validation让验证变得如此easy(一)
- jquery数组(排序)
- jquery 学习随笔 jquery.form.js
- jquery animate扩展
- jquery如何实现div可以随意移动
- MySQL Schema设计(四)一个MySQL里的JQuery:common_schema (先存着)
- jQuery form插件的使用--使用 fieldValue 方法校验表单
- 在smarty模板引入jquery