ITeye博客

不要滥用jQuery的$(this)

my14686 · 2011-11-11 23:09 · Web前端 · Java · jQuery · DOM · click · Markdown

如果不了解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方法

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。