JS:提交 HTML 文件并将文件代码放在 div 中 [英] JS: Submit HTML file and place file code within div

查看:35
本文介绍了JS:提交 HTML 文件并将文件代码放在 div 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我正在寻找的是一种让用户在点击时上传 html 文件的方式 按钮.

Basically what im looking for is a way for the user to upload an html file when they click <input type="file"/> button.

当用户选择一个 HTML 文件时,我想把 HTML 代码放在一个 div 中.这可以在没有服务器端语言的情况下完成吗?

When the user selects an HTML file, I want to take the HTML code and place it within a div. Can this be done without server-side languages?

推荐答案

使用 javascript 您无法浏览客户端磁盘上的文件.但是你的情况是可能的.请检查以下完全符合您要求的 stackover 问题.请参阅 Paolo Moretti 的回答.这是链接:如何使用 Javascript 打开本地磁盘文件?

Using javascript You cannot browse on a clients disk files. However your case is possible. Please check the below stackover question which exactly matches your requirement. Refer Paolo Moretti answer. Here is the link: How to open a local disk file with Javascript?

当您只想浏览/上传 HTML 文件时,将接受"添加到您的输入文件类型中,如下所示:

As you want to browse/upload only HTML files add "accept" to your input file type like this:

<input type="file" accept="text/html"/>

在 HTML5 中,您可以像这样使用文件扩展名作为接受:

In HTML5 you can use file extensions as the accept like this:

<input type="file" accept=".html"/>

这篇关于JS:提交 HTML 文件并将文件代码放在 div 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆