this和 $(this)的区别
来源:互联网 发布:淘宝上哪家卖玉的店铺 编辑:程序博客网 时间:2024/05/19 02:42
今天在使用jquery来实现一个小功能的时候,发现了一个小问题,故在这里总结一下,供以后学习》
问题:this 和 $(this)的区别
1.this 指的是html 对象
2.$(this) 指的是jquery对象
在使用$(this) 的时候要放到 $(function (){
$(this) 要写到这里。
});
下面来看一个例子:
$(function(){$(".choice1").click(function(){//$(this) 的用法 必须写到$(function(){}); 中 否则不生效var slideIndex = $(this).parent().parent().parent().attr("name");var choiceIndex = $(this).attr("name");alert("slideIndex:"+slideIndex);alert("choiceIndex:"+choiceIndex);});$(".choice2").click(function(){//$(this) 的用法 必须写到$(function(){}); 中 否则不生效var slideIndex = $(this).parent().parent().parent().attr("name");var choiceIndex = $(this).attr("name");alert("slideIndex:"+slideIndex);alert("choiceIndex:"+choiceIndex);});$(".choice3").click(function(){//$(this) 的用法 必须写到$(function(){}); 中 否则不生效var slideIndex = $(this).parent().parent().parent().attr("name");var choiceIndex = $(this).attr("name");alert("slideIndex:"+slideIndex);alert("choiceIndex:"+choiceIndex);});});
$(this) 的时候要放到 $(function (){
$(this) 要写到这里。
});
如果把代码改成是这样:
$(function(){$(".choice1").click(function(){choice1();});});function choice1(){//$(this) 的用法 必须写到$(function(){}); 中 否则不生效var slideIndex = $(this).parent().parent().parent().attr("name");var choiceIndex = $(this).attr("name");alert("slideIndex:"+slideIndex);alert("choiceIndex:"+choiceIndex);}
这样写就不对了 就回去不到$(this) 对象了 不知道为什么,现在这记录一下,以后有机会把它弄明白
阅读全文
0 0
- this和$(this)的区别
- this和 $(this)的区别
- this和$(this)区别
- JQuery 中this 和 $(this) 的区别
- JQuery -- this 和 $(this) 的区别
- JQuery this 和 $(this) 的区别
- JQuery -- this 和 $(this) 的区别
- JQuery -- this 和 $(this) 的区别
- JQuery -- this 和 $(this) 的区别
- jquery里面的$(this) 和this区别
- JQuery -- this 和 $(this) 的区别
- JQuery -- this 和 $(this) 的区别
- JQuery -- this 和 $(this) 的区别
- jquery $(this)和this的区别
- JQuery -- this 和 $(this) 的区别
- jquery中$this和$(this)的区别
- jquery中this 和 $this 的区别
- this.Invoke和this.BeginInvoke的区别
- HDU 6078 Wavel Sequence【动态规划】
- MFC+opencv遇到的问题汇总
- Android Studio导入第三方java类库含源代码包
- java集合之HashMap
- BZOJ 2243: [SDOI2011]染色 树链剖分 区间合并
- this和 $(this)的区别
- less-loader、css-loader、style-loader
- Unity3D利用Photon实现实时联网对战(一)Photon Sever介绍
- 密码加密
- vue单页面兄弟组件信息传递
- Android动态布局
- Hash算法解决冲突的方法一般有以下几种常用的解决方法
- c语言中printf("%x",-1);为什么会输出-1的十六进制补码??
- vim使用教程