我正在用Dojo 1.6编写一个可以修改属性值的dojo函数:
function replaceAttributeDojo(obj, attrName, newValue) {
var value = dojo.getAttr(obj, attrName);
if (value !== 'undefined') {
//console.log('Found attribute '+attrName+' on object '+obj.attr('nodeName')+'[id='+obj.attr('id')+', name='+obj.attr('name')+', widgetid='+obj.attr('widgetid')+']');
if (value == '') {
//console.log('Attribute value is empty, removing the attribute');
//obj.removeAttr(attrName);
return;
}
var newAttrValue = value.replace(/[\d]+/g, newValue);
dojo.setAttr(obj, attrName, newAttrValue);
} else {
//console.log('Did not find attribute '+attrName+' on object '+obj.attr('nodeName')+'[id='+obj.attr('id')+', name='+obj.attr('name')+', widgetid='+obj.attr('widgetid')+']');
}
}
跟踪代码并找到在这一行丢失的萤火虫
var value = dojo.getAttr(obj, attrName);
它告诉我
dojo.getAttr is not a function
该函数在dojo.ready之外定义,但在dojo.ready()
内部调用。
这是调用dojo.ready()
中的函数的部分dojo.query("div, input, select", row).forEach(function(){
replaceAttributeDojo(row, 'id' , index);
replaceAttributeDojo(row, 'name' , index);
replaceAttributeDojo(row, 'widgetid' , index);
});
dojo.ready()
内同一页上的所有内容都可以正常运行,那怎么可能呢?
答案 0 :(得分:2)
如果obj是DOM节点:
var val = dojo.attr(node, attrName); // getter
dojo.attr(node, attrName, newValue); // setter
http://dojotoolkit.org/reference-guide/1.6/dojo/attr.html
如果obj是一个小部件:
var val = widget.get(attrName); // getter
widget.set(attrName, newValue); // setter
http://dojotoolkit.org/reference-guide/1.8/dijit/_WidgetBase.html