0
点赞
收藏
分享

微信扫一扫

jQuery中this与$(this)的区别

圣杰 2022-03-25 阅读 91
前端jquery

1 jQuery中this与$(this)的区别

  • this指Dom对象或者JavaScript对象;
  • $this是将this转化为jQuary对象,可以直接调用jQuary方法;

代码示例:

var node = $('#id');
node.click(function(){
  this.css('display','block');  //报错  this是一个html元素,不是jquery对象,因此this不能调用jquery                             的css()方法
  $(this).css();      //正确   $(this)是一个jquery对象,不是html元素,可以用css()方法
  this.style.display = 'block';  //正确  this是一个html元素,不是jquery对象,因此this不能调用jquery的css()方法,但是可以用javascript来更改style属性
});
举报

相关推荐

0 条评论