[英]Call javascript function as array parameter
I have an array in my javascript funktion. 我的JavaScript函数中有一个数组。
var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];
This array is displayed into a html table multiple times. 此数组会多次显示到html表中。 The values should in the end be functions. 这些值最后应该是函数。 Right now they are all just placeholders. 现在,它们都只是占位符。 What I want is to set them as functions, so when I use data[0] it calls the function 'date();' 我想要的是将它们设置为函数,因此当我使用data [0]时,它将调用函数“ date();” which displays me the current date. 向我显示当前日期。 The function 'Schicht();' 函数“ Schicht();” should be called on the placeholders seen above. 应该在上面看到的占位符上调用。
EDIT: 编辑:
maybe I should post some more Information. 也许我应该发布更多信息。 So the array is put into an html table that i generate. 因此,将数组放入我生成的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');
}
And the date function for example that I try to call is: 例如,我尝试调用的日期函数是:
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);
}
------- ANSWER AFTER YOUR EDIT -------- -------编辑后的答案--------
Your function date
is not returning any value. 您的函数date
未返回任何值。 Try this version: 试试这个版本:
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;
}
Now this should work 现在这应该工作
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();
I think your question is related with How to execute a JavaScript function when I have its name as a string . 我认为您的问题与我以字符串形式命名的JavaScript函数有关。 You can use it like 你可以像这样使用它
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);
The other way is 另一种方法是
var Datum = function(args){
bla bla...
}
var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];
eval(data[0] + "(arguments)");
or you can directly add functions to array like: 或者您可以直接向数组添加函数,例如:
var Datum = function(){};
var Schicht = function(){};
var Data = [Datum, Schicht ...];
Data[0]();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.