JQuery -- this 和 $(this) 的区别
来源:互联网 发布:百度大数据交响乐 编辑:程序博客网 时间:2024/05/01 07:45
举个栗子:修改div里文本内容的颜色
<div id="box">DearMorning的博客</div>
①this,其实指向一个Html 元素。这里的this指向的box有Style对象属性,所以下面这样写是没有问题的。但是如果将this换成$(this)就Error了。
$("#box").hover( function() { this.style.color='red';});
②$(this),这里的$(this)是一个JQuery对象,而jQuery是一个类数组对象Array[element,element,.....],所以也就沒有style对象属性,因此这样写是错误
Error Code:
$("#box").hover( function() { $(this).style.color='red';//报错“Cannot set property 'color' of undefined”});
③$(this),可以将JQuery对象转化为DOM对象,再使用style对象属性
Right Code:
$("#box").hover( function() { $(this)[0].style.color='red'; //转化成DOM对象//$(this).get(0).style.color='red'; //通过get方法,转化成DOM对象});
④总结:DOM对象就是一个单独的DOM元素,jQuery是一个类数组对象
还可以用jQuery的方式更改对象属性
$("#box").hover( function(){$(this).css('color','red');});
0 0
- 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) 的区别
- git忽略某文件夹,但保留文件夹内的其它文件夹
- Git 常用技巧
- 《转》android利用socket与java后台交互的设计与实现
- Canvas画图设置渐变色
- Git操作指南
- JQuery -- this 和 $(this) 的区别
- Action 中获取表单数据的三种方式
- 中山大学转专业过程及心得
- urllib2 实现简单爬取12306网站
- hdu 2012 素数判定
- Python高级数据处理与可视化(三)---- Matplotlib图像属性控制
- 2007年北理复试上机题
- C++之继承篇
- socket.io简单笔记1