We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

HTMLSelectElement.selectedIndex

这篇翻译不完整。请帮忙从英语翻译这篇文章

The HTMLSelectElement.selectedIndex is a long that reflects the index of the first selected <option> element. The value -1 indicates that no element is selected.

Syntax

var index = selectElem.selectedIndex;
selectElem.selectedIndex = index;

Example

HTML

<p id="p">selectedIndex: 0</p>

<select id="select">
  <option selected>Option A</option>
  <option>Option B</option>
  <option>Option C</option>
  <option>Option D</option>
  <option>Option E</option>
</select>

JavaScript

var selectElem = document.getElementById('select')
var pElem = document.getElementById('p')

// When a new <option> is selected
selectElem.addEventListener('change', function() {
  var index = selectElem.selectedIndex;
  // Add that data to the <p>
  pElem.innerHTML = 'selectedIndex: ' + index;
})

Specifications

Specification Status Comment
HTML Living Standard
HTMLSelectElement
Living Standard No change since the latest snapshot, HTML5.
HTML5
HTMLSelectElement
Recommendation Initial definition, snapshot of HTML Living Standard.

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

 
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support ? ? ? ? ?
Feature Android Chrome Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support ? ? ? ? ? ? ?

文档标签和贡献者

此页面的贡献者: lanlingxiaoyizi
最后编辑者: lanlingxiaoyizi,