jQuery插件中为什么要return this.each?
来源:互联网 发布:js设置边框颜色 编辑:程序博客网 时间:2024/06/16 14:37
在Jquery插件中,经常会有return this.each(function(){})出现,那么这是为什么呢?看以下例子:
现在我想扩展jquery,写一个通用的方法,用来改变html元素中html内容的值;
1。未使用return this.each(function(){});
在Jquery插件中,经常会有return this.each(function(){})出现,那么这是为什么呢?看以下例子:现在我想扩展jquery,写一个通用的方法,用来改变html元素中html内容的值;1。未使用return this.each(function(){});
此时,我们得到的obj是undefined,也就是说如果我们想链式调用是不行的,如 :$(".test").addStr("add str").css("color","red")
2.使用return this.each(function(){});
<div class="test"> div1 </div> <div class="test"> div2 </div> <script type="text/javascript"> $.fn.addStr = function(str){ return this.each(function(){ this.innerHTML = str; } ) } var obj = $(".test").addStr("add str").css("color","red");<span style="color:#FF0000;">现在我们可以继续调用jquery对象的方法.css("","");</span> alert(obj instanceof jQuery);//true </script>而现在,我们能够取得返回的jquery对象,可以进行链式调用。
转载:http://blog.csdn.net/wyb_gg/article/details/53580216
阅读全文
0 0
- jQuery 插件为什么要return this.each()
- jQuery 插件为什么要return this.each()
- jQuery插件中为什么要return this.each?
- jQuery插件中为什么要return this.each?
- zepto 插件为什么访问this.each 呢?
- return this.each(function(){})
- jQuery.each 中的 return
- jquery ,each,return
- jquery.each return false 问题
- jquery的 each return 问题
- 浅谈jquery中的each方法$.each、this.each、$.fn.each
- Jquery的each里面return作用
- Jquery中each用法
- jQuery中跳出$.each
- jQuery中跳出$.each
- jquery中跳出each
- jquery中each用法
- JQuery中each解析
- springboot+dubbo+mybatis集成
- 计算机图形学-基于OpenGL的直线段的裁剪算法
- Deep Learning(深度学习)学习笔记整理系列之(六)
- Vue--- 子 --> 父 组件传递参数
- 宏与递归
- jQuery插件中为什么要return this.each?
- bzoj1751 [Usaco2005 qua]Lake Counting
- Deep Learning(深度学习)学习笔记整理系列之(七)
- 操作系统之进程同步
- Deep Learning(深度学习)学习笔记整理系列之(八)
- windows环境使用composer安装YII2.0
- tensorflow53 《面向机器智能的TensorFlow实战》笔记-05-01 卷积基础
- fl2440串口编程
- Java Web----实训项目第一阶段