this.id vs. $(this).attr('id')

前端 未结 3 670
北海茫月
北海茫月 2020-12-09 07:27

Is

$(this).attr(\'id\') 

the same as:

this.id
相关标签:
3条回答
  • 2020-12-09 08:22

    Almost (see Jeff's answer).

    jQuery abstracts away attribute getting, but it isn't always the most terse option.

    It is however shorter than getAttribute('id').

    0 讨论(0)
  • 2020-12-09 08:24

    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.

    0 讨论(0)
  • 2020-12-09 08:25

    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.

    0 讨论(0)
提交回复
热议问题