Skip to content

jQuery中,attr()和prop()的区别? #1

Description

@yoky

学习《DOM探索之基础详解篇》中,我们需要知道dom节点的类型,目的是知道操作dom的属性是用元素的直接属性操作还是使用setAttribute方法操作。
那么:
Q:什么是元素的直接属性?
A:元素天生自带的属性,比如id,class等。因此,我们coder自定义的属性就是非直接属性啦。

Q:如何获取??
A:直接属性使用dom对象可直接获取、修改。自定义属性使用setAttribute()设置、获取。
PS:setAttribute()方法也能设置dom的直接属性。同理,getAttribute()也能获取dom的直接属性。

引申:

Q:jQuery的attr和prop方法的区别?
A:attr()方法可以设置、获取jQuery对象的直接属性及自定义属性。prop()方法只能获取、设置jQuery对象的直接属性

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions