jQuery this.id 与 $(this).attr('id')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5597317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
this.id vs. $(this).attr('id')
提问by Phillip Senn
Is
是
$(this).attr('id')
the same as:
等同于:
this.id
回答by Jeff
No, they're not exactly the same.
不,它们并不完全相同。
They'll both return the element's ID, but if the element has no ID, then this.id
will return a blank string while $(this).attr("id")
will return undefined
.
它们都将返回元素的 ID,但如果元素没有 ID,this.id
则将返回一个空字符串,而$(this).attr("id")
将返回undefined
.
回答by alex
Almost (see Jeff's answer).
几乎(见杰夫的回答)。
jQuery abstracts away attribute getting, but it isn't always the most terse option.
jQuery 抽象了属性获取,但它并不总是最简洁的选择。
It is however shorter than getAttribute('id')
.
然而,它比 短getAttribute('id')
。
回答by Timbo
Same result, but this.id is much faster as it doesn't require all the jQuery stuff around it. You will also get different results if that item doesn't have an id.
相同的结果,但 this.id 快得多,因为它不需要它周围的所有 jQuery 东西。如果该项目没有 id,您也会得到不同的结果。