-1

如何在变量中保存 javascript 代码,我这样做了,但没有成功:

var reportD = '<html><head>';
reportD = reportD + '<script type="text/javascript" src="http://www.google.com/jsapi"></script>';
reportD = reportD + '<script type="text/javascript">';
reportD = reportD + 'google.load("visualization", "1", {packages: ["corechart"]});';
reportD = reportD + '</script>';
reportD = reportD + '<script type="text/javascript">';
reportD = reportD + 'function drawVisualization() {';
reportD = reportD + 'var data = google.visualization.arrayToDataTable([["x", "Cats", "Blanket 1", "Blanket 2"],["N",1,0.5,1]]);';
reportD = reportD + 'new google.visualization.LineChart(document.getElementById("visualization")).';
reportD = reportD + 'draw(data, {curveType: "function",width: 500, height: 400,vAxis: {maxValue: 10}});';
reportD = reportD + '}google.setOnLoadCallback(drawVisualization);';
reportD = reportD + "</script>";
reportD = reportD + '</head>';
reportD = reportD + '<body style="font-family: Arial;border: 0 none;">';
reportD = reportD + '<div id="visualization" style="width: 500px; height: 400px;"></div>';
reportD = reportD + '</body></html>';

我想这样做,以便将这些代码注入 iframe 元素

4

2 回答 2

1

script>标签内的字符串/注释/任何内容可能不包含</script>. 使用转义正斜杠\,它应该可以工作(即<\/script>

于 2012-04-26T22:08:58.763 回答
0

写你的html(记事本/你的ide),复制它转到

http://accessify.com/tools-and-wizards/developer-tools/html-javascript-convertor/

粘贴到您的 html 中,转换为 JS 变量。

于 2012-04-26T22:12:35.020 回答