将HTML代码存储在javascript中的字符串中
Store HTML code in a string in javascript
我正在将外部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="inner 2.html之后,它不会占用所有内容。有什么解决方案吗?
不能将多行文本存储在javascript字符串中。要将该html存储在javascript中,您必须转义引号并删除空白。一些例子:
这不起作用:
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";
您忘记转义引号:
<div id='"new'"> This id div text</div>
并用'n
替换新行
相关文章:
- 将DOM节点值与字符串Javascript进行比较
- 到UpperCase字符串Javascript中的特定字符索引
- 替换字符串javascript中的所有特殊字符..
- 在数组中查找某些字符串(Javascript)
- 解析特定的字符串Javascript(Node.js)
- 从字符串 JavaScript 中过滤多个正则表达式
- 在字符串 JavaScript 中使用正则表达式替换
- 忽略字符串javascript中的字符
- 如何检查url是否包含多个字符串.Javascript/jquery
- 截断字符串JavaScript
- 计算字符串javascript中的某些单词
- 从'获取本月的最后一天;2015-02-23'字符串(javascript)
- 从字符串javascript中获取键值对
- 从路径字符串 - Javascript 获取所有父路径
- 在字符串Javascript中查找丢失的字母
- AngularJS:如何使所有ng模型都是字符串.JavaScript和长整数
- 将浮点转换为至少有一个小数点的字符串(javascript)
- 替换字符串 JavaScript 中的多个文本
- 检查另一个字符串 JavaScript 中一个字符串是否有任何字符的最佳方法
- 将变量的值包含在 href 字符串 javascript 中