html⾃定义属性名称_HTML名称属性
html⾃定义属性名称
Despite its moniker, name is an unusual and often misunderstood . Its use is valid in the context of just over a dozen elements, only half of which are relevant to modern web development:
尽管名称绰绰有余,但name是⼀个不常见且经常被误解的 。 它的使⽤仅在⼗⼏个元素的上下⽂中有效,其中只有⼀半与现代Web开发相关:
<button>, <fieldset>, <form>, <iframe>, <input>, <output>, <select>, <textarea>
<button> , <fieldset> , <form> , <iframe> , <input> , <output> , <select> , <textarea>
As you can see, name is most commonly associated with , and it is in that context that most developers will encounter the attribute on a day-to-day basis.
如您所见, name最常与相关联,并且在这种情况下,⼤多数开发⼈员每天都会遇到该属性。
为什么使⽤名称? (Why Use Name?)
name is frequently employed to associate a form element with a value for a backend process, such as a form validation script. A simple example with radio buttons shows how the attribute is used:
name通常⽤于将表单元素与后端过程的值相关联,例如表单验证脚本。 ⼀个带有单选按钮的简单⽰例显⽰了如何使⽤该属性:
<form>
<fieldset>
<legend>Gender</legend>
<label>Male<input type="radio" value="m"></label>
<label>Female<input type="radio" value="f"></label>
<input type="submit" value="Go">
</fieldset>
</form>
In this code, the radio buttons in the form could both be turned on at the same time, and reported equally as valid values to the backend script. For the sake of simplicity, let’s assume that we want a binary response to gender*: we can’t use to gather a single value, as an id value must always be unique, so instead we’ll turn to name:
在此代码中,表单中的单选按钮可以同时打开,并作为有效值均等地报告给后端脚本。 为了简单起见,让我们假设我们想要对性别*的⼆进制响应:我们不能使⽤来收集单个值,因为id值必须始终是唯⼀的,所以我们将转向name :
<label>Male<input type="radio" value="m" name="gender"></label>
<label>Female<input type="radio" value="f" name="gender"></label>
Now not only do the radio buttons turn each other on and off, but the form now reports a single variable of gender to the backend script, with one of two possible values: m or f
现在,不仅单选按钮可以彼此打开和关闭,⽽且表单现在还向后端脚本报告了⼀个gender变量,并带有两个可能值之⼀: m或f
法规与变更 (Regulations & Variations)
It’s usually easiest to match the name and id values in form elements, for easier management of data:
通常最容易在表单元素中匹配name和id值,以简化数据管理:
<input type="text" name="personname" id="personname">
The oddities of name continue into its association with . In general, id is associated with front-end technologies (HTML, CSS and JavaScript) and name with backend (, Perl, etc). However, there’s a weird exception for . We’re very used to accessing elements in the DOM ElementById and , but these are relatively roundabout methods compared to what we can do with name. For example, in the completed form above:
name的奇异性继续与关联。 通常, id与前端技术(HTML,CSS和JavaScript)关联, name与后端( ,Perl等)关联。 但是, 有⼀个怪异的例外。 我们⾮常习惯通过ElementById和访问DOM中的元素,但是与使⽤name相⽐,这些⽅法相对来说⽐较these回。 例如,在上⾯完成的表格中:
<form onsubmit="alert(gender.value)">
In certain contexts, we can directly reference name in JavaScript: no need for a variable, and no long
syntax to remember. (ElementsByName() also works, except it returns an array).
网页计算器html代码在某些情况下,我们可以直接在JavaScript中引⽤name :不需要变量,也不需要长语法。 ( ElementsByName()也可以,但是它返回⼀个数组)。
This feature of name becomes particularly useful when dealing with relatively complex forms that use a lot of scripting, such as online calculators.
当处理使⽤⼤量脚本的相对复杂的表格(例如,在线计算器)时, name此功能特别有⽤。
html⾃定义属性名称
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论