我正在将外部html页面(1.html)的“视图源”复制到另一个html页面(2.html)中的javascript变量中。但是由于html页面中的缩进,引号,空格和标签,我无法一次性将所有源代码存储在字符串中。有没有可以用来做的功能?
1.html的内容:
<html>
<head>
<title>1 </title>
</head>
<body>
This is just plain text body
<div id="new"> This id div text</div>
<span> This is span text </span>
</body>
</html>
2.html的内容:
<html>
<head>
<script type="text/javascript" language="javascript">
var str="<html>
<head>
<title>2 </title>
</head>
<body>
This is just plain text body
<div id="new"> This id div text</div>
<span> This is span text </span>
</body>
</html>";
alert (str);
</script>
</head>
<body>
</body>
</html>
如果粘贴所有内容从1.html复制后,var =“在2.html内部,它不会全部采取它..任何解决方案吗?
答案 0 :(得分:3)
您无法在javascript字符串中存储多行文字。要在javascript中存储该html,您必须转义引号并删除空格。一些例子:
这不起作用:
var str = "Multiline
Text";
这不起作用:
var str = "Non-escaped text with "double quotes" and 'single quotes'";
这将有效:
var str = "This will work because the \"double quotes\" are escaped";
答案 1 :(得分:0)
你忘了逃避引用:
<div id=\"new\"> This id div text</div>
并用\n