<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.
Content categoriesFlow content, phrasing content.
Permitted contentEither phrasing content or zero or more <option> elements.
Tag omissionNone, both the starting and ending tag are mandatory.
Permitted parentsAny element that accepts phrasing content.
Implicit ARIA rolelistbox
Permitted ARIA rolesNo role permitted
DOM interfaceHTMLDataListElement

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

SpecificationStatusComment
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:33 次

字数:8269

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文