是否可以将数据所在的键作为参数传递给函数?
例如;
function myfirebasedata(myDivTag, value){
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.on('child_added', function(snapshot) {
var data = snapshot.val();
myDivTag.innerHTML = data.value.age;
});
}
然后我可以这样称呼:
myfirebasedata(divTagAgeJames, James);
myfirebasedata(divTagAgeBob, Bob);
....
....
例如,当函数的第一个实例运行/调用myfirebasedata(divTagAgeJames, James);
它应该替换上面firebase代码的引用中的关键字;
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.on('child_added', function(snapshot) {
var data = snapshot.val();
myDivTagJames.innerHTML = data.James.age;
});
更新
答案 0 :(得分:0)
您可以尝试其中之一:
ref.on('child_added', function(snapshot) {
var age = snapshot.child(value).val().age;
myDivTag.innerHTML = age;
});
ref.on('child_added', function(snapshot) {
var data = snapshot.val();
myDivTagJames.innerHTML = data[value].age;
});