简体   繁体   English

Javascript 模板文字和 Html 文本区域

[英]Javascript Template Literals and Html Textarea

To get some html to render nicely inside a textarea,为了让一些 html 在文本区域内很好地呈现,

在此处输入图像描述

I have to make my code-indentation look ugly;我必须让我的代码缩进看起来很难看; 在此处输入图像描述

What's the proper way to write my template literal, without resorting to bad indentation?在不使用错误缩进的情况下编写模板文字的正确方法是什么?

I'd use a separate script file instead, for which no indentation will be needed (or even appropriate) on the top level:我会改用一个单独的脚本文件,在顶层不需要(甚至不需要)缩进:

// script.js
const htmlStr =
`<section>
    ...
</section>`;
document.getElementById('txtHtmlStr').defaultValue = htmlStr;
<script src="./script.js"></script>

I'd prefer to separate script files from HTML files anyway, they can make things easier (eg, for proper separation of concerns, for having a single entry point, to permit linting, minifying, and transpiling)无论如何,我更愿意将脚本文件与 HTML 文件分开,它们可以使事情变得更容易(例如,为了适当的关注点分离,为了有一个单一的入口点,以允许 linting,缩小和转译)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM