简体   繁体   中英

Javascript Template Literals and Html Textarea

To get some html to render nicely inside a textarea,

在此处输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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