Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript外部作用域变量访问 OperationSelector=函数(selectElement){ this.selectElement=selectElement; } OperationSelector.prototype.populateSelectWithData=函数(xmlData){ $(xmlData).find('operation').each(function(){ var操作=$(此); selectElement.append(“”+operation.attr(“title”)+“”); }); }_Javascript_Scope - Fatal编程技术网

Javascript外部作用域变量访问 OperationSelector=函数(selectElement){ this.selectElement=selectElement; } OperationSelector.prototype.populateSelectWithData=函数(xmlData){ $(xmlData).find('operation').each(function(){ var操作=$(此); selectElement.append(“”+operation.attr(“title”)+“”); }); }

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.selectElement?

在迭代函数之前,将其分配给函数范围中的局部变量。然后您可以在以下内容中引用它:

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")));
    }); 
}