教程集 www.jiaochengji.com
教程集 >  jQuery  >  jquery 教程  >  正文 jquery $(this)和this的区别

jquery $(this)和this的区别

发布时间:2015-11-17   编辑:jiaochengji.com
通过实例了解下jquery中$(this)和this的区别,有需要的朋友参考下。

jquery中this与$(this)的区别:
this 指代的是 DOM 对象,而$(this)指代的是 jQuery 对象。

相于这样的区别:
 

复制代码 代码示例:
var a=document.getElementsByTagName(''a'')[0];
var $a=$(''a'').eq(0);

例子:
 

复制代码 代码示例:

<body>
    <a href="http://www.baidu.com/"></a>
    <a href="http://www.jbxue.com/wb/php/"></a>
</body>

<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(''a'').each(function(){
        console.log(this.href);
        console.log($(this).href);
    });
</script>

效果如下图:
jquery $(this)

因为$(this)没有href属性,所以会显示undefined;如果使用$(this)。
正确写法:
$(this).attr(''href'');

您可能感兴趣的文章:
jquery $(this)和this的区别
jQuery中需要注意的细节问题小结
JQuery this 和 $(this) 的区别
基于Jquery的表格隔行换色,移动换色,点击换色插件
php中self与$this的用法区别
实例详解php中self、parent、this的区别
jquery中dom操作和事件的实例学习 仿yahoo邮箱登录框的提示效果
target与currentTarget的区别(jqueryui方式获取z-Index)
jQuery 验证插件 Web前端设计模式(asp.net)
jquery标签页效果示例代码

[关闭]
~ ~