我在 svelte 中编写了这段代码,打开一个 txt 文件,并将其写入 html 段落。这是代码:
<input type='file' multiple bind:files'>
{#if files}
<h2>Files selected: </h2>
{#each Array.from(files) as file}
<p> IThe imported file is: {file.name}</p>
{#await file.text() then text}
<p>{text}</p>
{/await}
{/each}
{/if}
Run Code Online (Sandbox Code Playgroud)
示例文件如下:
beans
spam
donut
Run Code Online (Sandbox Code Playgroud)
但它给了我这个:
beans spam donut
Run Code Online (Sandbox Code Playgroud)
如何<br>为文本中的每一行创建一个新行(段落或 )?
这个怎么样?只需在换行符上拆分并创建一个新的 p 标签即可。
<script>
let files;
</script>
<input type='file' multiple bind:files />
{#if files}
<h2>Files selected: </h2>
{#each Array.from(files) as file}
<p> IThe imported file is: {file.name}</p>
{#await file.text() then text}
{#each text.split('\n') as line }
<p>{line}</p>
{/each}
{/await}
{/each}
{/if}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1811 次 |
| 最近记录: |