我的javascript funktion中有一个数组。
var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];
此数组多次显示在html表中。这些值最终应该是函数。 现在他们都只是占位符。我想要的是将它们设置为函数,因此当我使用data [0]时,它会调用函数' date();'它显示当前日期。功能' Schicht();'应该在上面看到的占位符上调用。
编辑:
也许我应该发布更多信息。所以数组被放入我生成的html表中。
function tableCreate(){
var header = ['Datum','VoMi +D','Vo + NaMi','NaMi I','NaMi Dispo','Abend I/a','Abend I/b','Abend II','Abend III','Abend IV','Abend V','Abend Dispo'];
var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];
var html = '<table><thead><tr>';
for (var x=0; x < 12; ++x){
html += '<th>' + header[x] + '</th>';
}
html += '</tr></thead><tbody>';
for (var i = 0; i < 28; ++i) {
html += '<tr>';
for (var j = 0; j < 12; ++j) {
//var func = windows[data[j]];
html += '<td>' + [data[j] + '</td>';
}
html += "</tr>";
}
html += '</tbody></table>';
$(html).appendTo('#tabs1-schicht');
}
我尝试调用的日期函数是:
var date = function date(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = dd+'.'+mm+'.'+yyyy;
console.write(today);
}
答案 0 :(得分:0)
-------编辑后的答案--------
您的函数date
未返回任何值。试试这个版本:
var date = function (){
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth()+1, //January is 0!
yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = dd+'.'+mm+'.'+yyyy;
if(console.log) console.log(today);
return today;
}
现在这应该可行
function test(){
var data = [];
data.push(date);
var html = "<table><tr><td>" + data[0]() + "</td></tr></table>";
if(console.log)console.log(html); // in chorme
}
test();
答案 1 :(得分:0)
我认为您的问题与How to execute a JavaScript function when I have its name as a string有关。您可以像
一样使用它var Datum = function(args){
bla bla..
}
var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];
var func = window[data[0]];
func(arguments);
另一种方式是
var Datum = function(args){
bla bla...
}
var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];
eval(data[0] + "(arguments)");
或者您可以直接将函数添加到数组中,如:
var Datum = function(){};
var Schicht = function(){};
var Data = [Datum, Schicht ...];
Data[0]();