Best practice for HTML

2019-04-04 18:35发布

What is the best practice to pre-select an <option> in a <select>?

According to different websites, both works. But which one is better? the most compatible?

<option selected="selected">Foo</option>
<option selected>Bar</option>

2条回答
对你真心纯属浪费
2楼-- · 2019-04-04 18:59

According to the HTML4 standard, using selected implies it is selected="selected". So the standard already defines both are supported. Their code sample already includes a code block just using selected.

For compatibility with XHTML (specifically XML is the 'problem' here), there is a selected="selected" option.

HTML5 also defines selected on its own is valid, so no need to worry.

查看更多
家丑人穷心不美
3楼-- · 2019-04-04 19:08

If you are writing XHTML, selected="selected" is required.

If you are writing HTML, selected is fewer keystrokes and fewer bytes.

Neither has better compatibility with browsers.

查看更多
登录 后发表回答