JQuery -- this 和 $(this) 的区别
来源:互联网 发布:淘宝帐号查询 编辑:程序博客网 时间:2024/05/21 17:43
起初以为this和$(this)就是一模子刻出来。但是我在阅读时,和coding时发现,总不是一回事。
What is "this"?
In many object-oriented programming languages, this (or self) is a keyword which can be used in instance methods to refer to the object on which the currently executing method has been invoked.
$("#textbox").hover(
function() {
this.title = "Test";
},
fucntion() {
this.title = "OK”;
}
);
这里的this其实是一个Html 元素(textbox),textbox有text属性,所以这样写是完全没有什么问题的。
但是如果将this换成$(this)就不是那回事了,Error--报了。
Error Code:
$("#textbox").hover(
function() {
$(this).title = "Test";
},
function() {
$(this).title = "OK";
}
);
这里的$(this)是一个JQuery对象,而jQuery对象沒有title 属性,因此这样写是错误的。
JQuery拥有attr()方法可以get/set DOM对象的属性,所以正确的写法应该是这样:
正确的代码:
$("#textbox").hover(
function() {
$(this).attr(’title’, ‘Test’);
},
function() {
$(this).attr(’title’, ‘OK’);
}
);
- 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 的区别
- JQuery -- this 和 $(this) 的区别
- JQuery中$(this)和this的区别
- JQuery -- this 和 $(this) 的区别
- JQuery -- this 和 $(this) 的区别
- intval — 获取变量的整数值
- jQuery - last-child 选择所有最后一个子元素 last最后一个元素
- $jquery.ajax 访问数据库 php html 等。
- jquery 选择奇偶行。
- JQuery中$.ajax()方法参数详解
- JQuery -- this 和 $(this) 的区别
- 数据结构7—线索二叉树 (java)
- 基类中this指针和派生类对象的关系,同名隐藏原则和作用域的示例
- jquery css()方法
- GourdScan被动扫描环境搭建
- jquery ajax 单击修改文档
- app跳转到应用市场打分
- 微信接口 菜单 40016
- arclist标签时,想按照权重越大越靠前来排序(58nin.com原创)