<datalist>: The HTML Data List element - HTML: HyperText Markup Language 编辑
The HTML <datalist>
element contains a set of <option>
elements that represent the permissible or recommended options available to choose from within other controls.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.Content categories | Flow content, phrasing content. |
---|---|
Permitted content | Either phrasing content or zero or more <option> elements. |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts phrasing content. |
Implicit ARIA role | listbox |
Permitted ARIA roles | No role permitted |
DOM interface | HTMLDataListElement |
Attributes
This element has no other attributes than the global attributes, common to all elements.
Examples
Basic datalist
<label for="myBrowser">Choose a browser from this list:</label>
<input list="browsers" id="myBrowser" name="myBrowser" />
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Internet Explorer">
<option value="Opera">
<option value="Safari">
<option value="Microsoft Edge">
</datalist>
Result
Customizing datalist styles
The style of the list produced by a <datalist>
element is platform-dependent, so if you want to customize the display of the options, you have to implement your own custom solution that overrides the default behavior. The below example provides a simple solution for this. Note how we haven't specified the <datalist>
id
inside the <input>
list
attribute in this case, as that stops the browser-specific display of the data list items from kicking in. Instead, we are setting the selected <datalist>
value in the <input>
via JavaScript.
Note: Since ::after
is not permitted on <input>
elements, if you want to reproduce the arrow-down icon you will have to wrap the <input>
element in another element that you can hang the styling on (or some other suitable solution).
HTML
<input
list=""
name="option"
id="input"
autocomplete="off"
>
<datalist id="datalist">
<option>Carrots</option>
<option>Peas</option>
<option>Beans</option>
</datalist>
CSS
datalist {
position: absolute;
background-color: lightgrey;
font-family: sans-serif;
font-size: 0.8rem;
}
option {
background-color: #bbb;
padding: 4px;
margin-bottom: 1px;
cursor: pointer;
}
JavaScript
input.onfocus = function () {
datalist.style.display = 'block';
}
for (let option of datalist.options) {
option.onclick = function () {
input.value = this.value;
datalist.style.display = 'none';
}
}
datalist.style.width = input.offsetWidth + 'px';
datalist.style.left = input.offsetLeft + 'px';
datalist.style.top = input.offsetTop + input.offsetHeight + 'px';
Result
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of '<datalist>' in that specification. | Living Standard | |
HTML5 The definition of '<datalist>' in that specification. | Recommendation |
Browser compatibility
BCD tables only load in the browser
Polyfill
Include this polyfill to provide support for older and currently incompatible browsers:
datalist-polyfill
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论