What is the difference between properties and attributes in HTML?

文章讲述了HTML元素的属性如何在DOM节点中体现为对象的属性。例如,<input>元素的id、type和value在DOM节点中都有对应属性。id和type是纯反射属性,而value属性反映的是输入框的当前值,不完全等同于HTML源码中的初始值。defaultValue属性则代表了元素的初始值。文章还提到了其他属性与它们的限制或修改情况。

When writing HTML source code, you can define attributes on your HTML elements. Then, once the browser parses your code, a corresponding DOM node will be created. This node is an object, and therefore it has properties.

For instance, this HTML element:

<input type="text" value="Name:">

has 2 attributes (type and value).

Once the browser parses this code, a HTMLInputElement object will be created, and this object will contain dozens of properties like: accept, accessKey, align, alt, attributes, autofocus, baseURI, checked, childElementCount, childNodes, children, classList, className, clientHeight, etc.

For a given DOM node object, properties are the properties of that object, and attributes are the elements of the attributes  property of that object. 对于一个DOM节点对象,property就是这个对象上的属性;attribute是该对象对应的HTML标签元素上的属性。

When a DOM node is created for a given HTML element, many of its properties relate to attributes with the same or similar names, but it's not a one-to-one relationship. For instance, for this HTML element:

<input id="the-input" type="text" value="Name:">

the corresponding DOM node will have id,type, and value properties (among others):

  • The id property is a reflected property for the id attribute: Getting the property reads the attribute value, and setting the property writes the attribute value. id is a pure reflected property, it doesn't modify or limit the value.

  • The type property is a reflected property for the type attribute: Getting the property reads the attribute value, and setting the property writes the attribute value. type isn't a pure reflected property because it's limited to known values (e.g., the valid types of an input). If you had <input type="foo">, then theInput.getAttribute("type") gives you "foo" but theInput.type gives you "text".

  • In contrast, the value property doesn't reflect the value attribute. Instead, it's the current value of the input. When the user manually changes the value of the input box, the value property will reflect this change. So if the user inputs "John" into the input box, then:

    theInput.value // returns "John"
    

    whereas:

    theInput.getAttribute('value') // returns "Name:"
    

    The value property reflects the current text-content inside the input box, whereas the value attribute contains the initial text-content of the value attribute from the HTML source code.

    So if you want to know what's currently inside the text-box, read the property. If you, however, want to know what the initial value of the text-box was, read the attribute. Or you can use the defaultValue property, which is a pure reflection of the value attribute:

    theInput.value                 // returns "John"
    theInput.getAttribute('value') // returns "Name:"
    theInput.defaultValue          // returns "Name:"
    

There are several properties that directly reflect their attribute (relid), some are direct reflections with slightly-different names (htmlFor reflects the for attribute, className reflects the class attribute), many that reflect their attribute but with restrictions/modifications (srchrefdisabledmultiple), and so on.

### 题目重述 以下是一组关于C++中指针的控制性问题,涉及指针的基本概念、操作符、初始化、数组关系、指针运算、const修饰、迭代安全、空指针及数组访问机制。 --- ### 详解 **1. 什么是C++中的指针?它的两个主要属性是什么?** 指针是存储变量内存地址的变量。其两个主要属性为: - **值(value)**:所指向内存地址的内容(通过`*ptr`访问)。 - **地址(address)**:指针自身在内存中的位置(通过`&ptr`获取)。 --- **2. `&` 和 `*` 操作符在指针中的区别是什么?** - `&` 是取地址操作符,返回变量的内存地址,如 `&var`。 - `*` 是解引用操作符,访问指针所指向地址的值,如 `*ptr`。 --- **3. 为什么指针初始化至关重要?未初始化的指针有哪些危险?** 未初始化的指针包含随机内存地址,称为“野指针”。 若解引用会导致**程序崩溃**或**未定义行为**,可能破坏系统数据。 --- **4. C++中数组与指针的基本关系是什么?** 数组名在大多数情况下可被解释为指向其首元素的指针。 例如:`int arr[5];` 中,`arr` 等价于 `&arr[0]`。 --- **5. 指针运算如何工作?为何 `ptr + 1` 增加的是所指类型的大小而非1字节?** 指针运算基于所指数据类型的大小自动缩放。 若 `ptr` 指向 `int`(4字节),则 `ptr + 1` 实际增加 $4$ 字节,确保指向下一个有效元素。 --- **6. 数组名和指针变量的区别是什么?为何不能对数组名进行自增?** 数组名是一个**常量指针**,代表首元素地址,不可修改。 而指针变量是可变的左值,可执行 `p++`;数组名 `arr++` 非法,因其地址固定。 --- **7. `const int*`、`int* const` 和 `const int* const` 的区别?** - `const int*`:指向常量的指针,可改指针,不可改值。 - `int* const`:常量指针,不可改指针,可改值。 - `const int* const`:指向常量的常量指针,均不可更改。 --- **8. 如何用指针安全遍历数组?边界风险有哪些?** 使用指针从首地址遍历到末地址: ```cpp for(int* p = arr; p < arr + n; ++p) ``` 风险:越界访问会导致**未定义行为**,必须严格控制终止条件。 --- **9. 什么是空指针?为何解引用前应检查是否为 `nullptr`?** 空指针不指向任何有效内存(值为 `nullptr` 或 `NULL`)。 解引用空指针会引发**运行时错误**或**段错误(segmentation fault)**。 --- **10. 如何用指针语法访问数组元素?编译器如何翻译 `arr[i]`?** 可用 `*(arr + i)` 访问第i个元素。 编译器将 `arr[i]` 内部转换为 `$*(arr + i)$`,利用指针算术实现。 --- ### 知识点 - **指针与内存地址**:指针保存变量地址,通过`*`和`&`实现地址操作与值访问。 - **指针运算机制**:`ptr + n` 实际移动 `$n \times \text{sizeof(type)}$` 字节。 - **数组与指针等价性**:数组名可作指针使用,但为不可修改的常量地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值