`
my14686
  • 浏览: 9482 次
文章分类
社区版块
存档分类
最新评论

不要滥用jQuery的$(this)

阅读更多
如果不了解javasrcipt中基本的DOM属性和方法的话,很容易滥用jQuery对象。比如: $('#someAnchor').click(function() { alert( $(this).attr('id') ); }); 如果你只是通过jQ对象获取简单的dom元素的属性比如id,那么你完全可以使用js原生的方法: $('#someAnchor').click(function() { alert( this.id ); }); 诸如“src,” “href,” 和“style.”等一些属性在...
如果不了解javasrcipt中基本的DOM属性和方法的话,很容易滥用jQuery对象。比如:
$(‘#someAnchor’).click(function() {
alert( $(this).attr(‘id’) );
});
如果你只是通过jQ对象获取简单的dom元素的属性比如id,那么你完全可以使用js原生的方法:
$(‘#someAnchor’).click(function() {
alert( this.id );
});
诸如“src,” “href,” 和“style.”等一些属性在老版本的ie中使用了getAttribute方法
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics