Javascript外部作用域变量访问 OperationSelector=函数(selectElement){ this.selectElement=selectElement; } OperationSelector.prototype.populateSelectWithData=函数(xmlData){ $(xmlData).find('operation').each(function(){ var操作=$(此); selectElement.append(“”+operation.attr(“title”)+“”); }); }
如何访问迭代块中的OperationSelector.selectElement?在迭代函数之前,将其分配给函数范围中的局部变量。然后您可以在以下内容中引用它:Javascript外部作用域变量访问 OperationSelector=函数(selectElement){ this.selectElement=selectElement; } OperationSelector.prototype.populateSelectWithData=函数(xmlData){ $(xmlData).find('operation').each(function(){ var操作=$(此); selectElement.append(“”+operation.attr(“title”)+“”); }); },javascript,scope,Javascript,Scope,如何访问迭代块中的OperationSelector.selectElement?在迭代函数之前,将其分配给函数范围中的局部变量。然后您可以在以下内容中引用它: OperationSelector = function(selectElement) { this.selectElement = selectElement; } OperationSelector.prototype.populateSelectWithData = function(xmlData) { $(x
OperationSelector = function(selectElement) {
this.selectElement = selectElement;
}
OperationSelector.prototype.populateSelectWithData = function(xmlData) {
$(xmlData).find('operation').each(function() {
var operation = $(this);
selectElement.append('<option>' + operation.attr("title") + '</option>');
});
}
顺便说一句,您通常不应该使用HTML字符串来创建新选项。如果
标题
可能包含
OperationSelector = function(selectElement) {
this.selectElement = selectElement;
}
OperationSelector.prototype.populateSelectWithData = function(xmlData) {
var os = this;
$(xmlData).find('operation').each(function() {
var operation = $(this);
os.selectElement.append(new Option(operation.attr("title")));
});
}