は、私がバッククォートを使用するときに警告メッセージが正しく表示されないことに注意:バックトリックを使用しているときにJavaScript警告内にテキストを正しく表示するにはどうすればよいですか?
例:
var classicMsg = "Lorem Ipsum is simply dummy text of the printing and typesetting "
\t \t \t + "ndustry. Lorem Ipsum has been the industry's standard dummy "
\t \t \t + "text ever since the 1500s, when an unknown printer took a galley "
\t \t \t + "of type and scrambled it to make a type specimen book.";
var backticksMsg = `Lorem Ipsum is simply dummy text of the printing and typesetting
ndustry. Lorem Ipsum has been the industry's standard dummy
text ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book.`;
document.getElementById("btnAlert1").addEventListener('click', function(){
alert(classicMsg);
});
document.getElementById("btnAlert2").addEventListener('click', function(){
\t alert(backticksMsg);
});
li{cursor: pointer;}
<ul>
<li id="btnAlert1">Test with classic message</li>
<li id="btnAlert2">Test with backticks message</li>
</ul>
を正しく警告バッククォートを表示するための良い方法はありますそれを行うためのカスタム関数を書くよりも、テキストが... ...私はこのようなことをしないと意味します:
function formatTextBeforeCallingAlert(txt) {
return txt.replace(/\n/g, '').replace(/\t/g, '');
}
"ソースに挿入された任意の改行文字はリテラルテンプレートの一部です。" - [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) - 改行が必要な場合を除き、改行を追加しないでください。 – Gerrit0
@ gerrit0しかし、私は "補間"を使って変数の内容を挿入する利点を失いたくはありません... –
新しい行を削除すると補間ができないのはなぜですか? – charlietfl