JavaScript反引号多行字符串在Internet Explorer中不起作用 [英] JavaScript backtick multiline string not working in Internet Explorer
问题描述
我在var
中包含一个很大的HTML字符串.我正在用它写到innerHTML
.
I have a large HTML string contained in a var
. I'm using it to write to innerHTML
.
最简单的第一个示例(带有反引号语法)在Internet Explorer 11中不起作用.
The first example (with backtick syntax), which is the simplest, does not work in Internet Explorer 11.
有没有一种方法可以使第一个示例在Internet Explorer 11中工作而不必使用数组或换行符?
Is there a way to get the first example to work in Internet Explorer 11 without having to use an array or newline characters?
反引号`
https://jsfiddle.net/qLm02vks/
<div id="display"></div>
var message = `
<p>this</p>
<p>is</p>
<p>a</p>
<p>multiline</p>
<p>string</p>
`;
// Write Message
var display = document.getElementById('display');
display.innerHTML = message;
在Internet Explorer中工作
数组连接
Works in Internet Explorer
Array Join
https://jsfiddle.net/3aytojjf/
var message =
['<p>this</p>',
'<p>is</p>',
'<p>a</p>',
'<p>multiline</p>',
'<p>string</p>'
].join('\n');
在Internet Explorer中工作
单引号'和换行符\
Works in Internet Explorer
Single quote ' with linebreak \
https://jsfiddle.net/5qzLL4j5/
var message =
'<p>this</p> \
<p>is</p> \
<p>a</p> \
<p>multiline</p> \
<p>string</p>'
;
推荐答案
问题
字符串的反引号语法是 Template文字 ,它允许对字符串和多行字符串中的变量进行插值. Internet Explorer 11不支持它们(请参阅此处的更多信息: ECMAScript 6兼容性表).
- 您可以使用转译器,例如广受欢迎的 Babel .这样会将模板文字转换为Internet Explorer 11可以理解的ECMAScript 5语法.
- 您可以选择不支持Internet Explorer 11,并继续支持Edge和其他具有本机ECMAScript 6支持的浏览器,尽管通常这不是一个选择. li>
- You can use a transpiler, such as the ever-popular Babel. This will convert the template literal into the ECMAScript 5 syntax that Internet Explorer 11 understands.
- You could opt-out of supporting Internet Explorer 11, and stick with support for Edge and other browsers that have native ECMAScript 6 support, though this is usually not an option.
这篇关于JavaScript反引号多行字符串在Internet Explorer中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!