【发布时间】:2012-08-06 15:48:15
【问题描述】:
所以我有一个将字符串写入 div 的函数。 该字符串被声明为content,由html代码组成,如下所示:
<h2>This is a test result</h2>
<script type="text/javascript" src="example_content.js"></script>
<script type="text/javascript"> document.write(example_content); </script>
在哪里 example_content.js :
example_content = '<p>some text some text some text some text some text</p>'
+ '<p>some more text....</p>'
+ '<hr />'
+ '<p>End of text</p>';
问题是,而不是在 div 中获取这个:
<h2>This is a test result</h2>
<p>some text some text some text some text some text</p>
<p>some more text....</p>
<hr />
<p>End of Text</p>
从字面上看,我最终是这样的:
<h2>This is a test result/h2>
<script type="text/javascript" src="example_content.js"></script>
<script type="text/javascript"> document.write(example_content); </script>
有没有办法避免将脚本标签写成字符串?但作为一个实际的脚本?
提前感谢您的帮助:)
【问题讨论】:
-
不能把
document.write(example_content);放到js文件里面吗? -
您是否尝试将 HTML 代码放入脚本标签中?对不起,我没有得到这个问题。
-
@Дамян Станчев 不过,如果不会写成脚本,就写成字符串。
标签: javascript file function include eval