您的位置:首页 > Web前端 > JQuery

jQuery中this与$(this)的区别

2015-08-30 07:55 603 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="style/css.css" rel="stylesheet" type="text/css" />

<script src="js/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function(){
console.log("ready");
$("#textbox").hover(
function(){
console.log("fdsfds");
this.title="Test";
},
function(){
this.title="OK";
}
);
});
</script>

</head>
<body>
<p id="textbox">pppppppppppp</p>
</body>
</html>


这里的this其实是一个Html 元素(textbox),textbox有title属性,所以这样写是完全没有什么问题的。
但是如果将this换成$(this)就不是那回事了,Error--报了。$(this)表示一个jquery对象。this与$(this)的区别在此。

要用$(this),得这样写。

$("#textbox").hover(
function() {
$(this).attr(’title’, ‘Test’);
},
function() {
$(this).attr(’title’, ‘OK’);
}
);


一个移动设备触控滑块的js很不错的样子:

http://www.swiper.com.cn/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: