JavaScript在jsfiddle.net上无法运行。
JavaScript在jsfiddle.net上无法运行。
下面的代码在实时网站上运行正常,但无法在jsfiddle上运行。
请参考此处的示例。
有人能告诉我为什么它在jsfiddle上无法运行吗?
在控制台上记录了: ReferenceError:fillList未定义
和 ReferenceError:mySelectList未定义
。
代码正常工作,可以在此处嵌入片段中看到:
function BetterSelect(oSelList) { this.objSelectList = oSelList; this.objSelectList.onchange = this.selectionChanged; } BetterSelect.prototype.clear = function() { this.objSelectList.options.length = 0; } BetterSelect.prototype.fill = function(aValues) { this.clear(); for (var i = 0; i < aValues.length; i++) { this.objSelectList.options[i] = new Option(aValues[i]); } } BetterSelect.prototype.find = function(strToFind, bSelect) { var indx = -1; this.objSelectList.options.selectedIndex = -1; for (var i = 0; i < this.getCount(); i++) { if (this.objSelectList.options[i].text == strToFind) { indx = i; if (bSelect) this.objSelectList.options.selectedIndex = i; } } return indx; } BetterSelect.prototype.getCount = function() { return this.objSelectList.options.length; } BetterSelect.prototype.selectionChanged = function() { alert("selection changed!"); } var mySelectList = null; window.onload = function() { mySelectList = new BetterSelect(document.getElementById('theList')); } function fillList() { mySelectList.fill(["one", "two", "three", "four", "five"]); } function findIt() { mySelectList.find(document.getElementById('txtToFind').value, true); }