IEではフォーム内の要素にIDを付けてもグローバル変数として定義されない
- サンプルコード
<!DOCTYPE html> <title>TEST</title> <body> <input id="input1"> <form> <input id="input2"> </form> <pre><script> function w(text) { document.writeln(text); } try { w('input1: ' + input1); } catch (e) { w('input1 is undefined'); } try { w('input2: ' + input2); } catch (e) { w('input2 is undefined'); } </script></pre>
- 結果
-
input1 input2 IE 10 [object HTMLInputElement] undefined Firefox 18 [object HTMLInputElement] [object HTMLInputElement] Chrome 24 [object HTMLInputElement] [object HTMLInputElement] Opera 12.12 [object HTMLInputElement] undefined
Windows ストアアプリを作っているときにはまった。IntelliSenseで補完候補としてinput2が出てくるものの、実際に使うと Reference Error になる。
HTML Standard では 6.2.4 Named access on the Window object がこれに関しての説明だと思うんだけど、form内の要素については特に何も書いていないように見える。
msdnのどこかに書いてるのかな……。