任何人都知道这段代码中的提示ID是什么意思?
var ui = ["input","prompt","heading"]; // An array of element ids
ui.forEach(function(id) { // For each id look up the element
ui[id] = document.getElementById(id); // and store it in a property
});
运行此代码后,ui.input,ui.prompt和ui.heading指的是文档元素。 脚本可以使用全局变量input和heading而不是ui.input 和ui.heading。但是Window对象有一个名为的方法 prompt(),因此脚本不能使用全局变量提示而不是ui.prompt。
相反,使用document.getElementById()显式查找元素。使用这个
var $ = function(id) { return document.getElementById(id); };
ui.prompt = $("prompt");
现有提示有ID吗?为什么脚本不能使用全局变量提示?