为什么DOM中不会显示多个HTML选择标记?

Spa*_*ude 2 html xhtml dom html-select

我正在使用jQuery在页面加载时动态填充一些级联下拉控件.但是,当页面加载时,我的三个选择框中只有两个被识别.

请考虑以下HTML:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html
 xmlns="http://www.w3.org/1999/xhtml"> <head>
     <title></title> </head> <body>
     <select id="one" />
     <select id="two"  />
     <select id="three"  /> </body> </html>
Run Code Online (Sandbox Code Playgroud)

当我在IE,Firefox或Chrome中打开此页面时,只会呈现两个选择框.三个中只有两个出现在DOM中(使用萤火虫或类似物),但这三个都出现在源中.

我可以做些什么来显示所有控件?

Que*_*tin 6

有效,HTML兼容标记.

您的选择元素应具有显式结束标记和至少一个选项元素.

<select id="one"><option>foo</option></select>
<select id="two"><option>foo</option></select>
<select id="three"><option>foo</option></select>
Run Code Online (Sandbox Code Playgroud)


kap*_*apa 5

简单:编写有效的HTML代码.浏览器正在尽力解析无效的HTML,但有时它们只会扼杀它.

<select>它不是一个自动关闭的标签,它必须有一个结束标签和至少一个optionoptgroup元素.

<select id="one"><option></option></select>
<select id="two"><option></option></select>
<select id="three"><option></option></select>
Run Code Online (Sandbox Code Playgroud)

jsFiddle演示

  • @SpazDude - Parse引擎基于Content-Type而不是Doctype进行切换.当他们点击Doctype时,为时已晚(除非他们从头开始重新解析). (3认同)
  • 虽然自闭标签有效,但XHTML Transitional DTD具有以下定义:<!ELEMENT select(optgroup | option)+>`这意味着您必须在select元素中包含LEAST 1 optgroup OR选项元素. (2认同)