我有以下Javascript函数,我希望通过将字符串与整数连接来动态引用InnerHTML中的变量,但这似乎不起作用(返回一个字符串):
function TestFunction() {
var activity_1 = "Hello World";
var i = 1;
var x = 'activity_' + i;
document.getElementById("myTest").innerHTML = x;
}
提前致谢。
答案 0 :(得分:3)
请检查此代码并告诉我它是否正常工作。
case (Reporter.PDF): {
Logging.info("pdf");
exporter = new JRPdfExporter();
response.addHeader("Content-Disposition", "inline; filename="
+ reportName + ".pdf");
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.exportReport();
outputStream.flush();
outputStream.close();

function TestFunction() {
var obj = {activity_1:"Hello World"};
var i = 1;
var x = "activity_" + i;
document.getElementById("myTest").innerHTML = obj[x];
}
TestFunction();

答案 1 :(得分:1)
使用对象文字存储对变量的多个引用
var test = {
activity_1: "Hello World",
i: 1
}
var x = test.activity_1+' '+ test.i;
document.getElementById("xTest").innerHTML = x;
<div id='xTest'></div>