Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从文件导入JS和HTML?_Javascript_Html - Fatal编程技术网

Javascript 如何从文件导入JS和HTML?

Javascript 如何从文件导入JS和HTML?,javascript,html,Javascript,Html,在js文件中找到导入示例 请调整它,使代码保存在js文件中 您好,请帮助在js文件中找到导入示例。请调整它,以便代码保存在js文件中 桌子 函数myim(){ a、 innerHTML=在b.js中导入代码 } 您可以在javascript中使用XMLHttpRequest从服务器请求数据。 file.open(“GET”,“b.js”)可用于打开文件。 file.responseText用于从该文件获取数据 注意: 在web服务器(本地或在线)上使用此代码可以避免某些浏览器中的CORS策

在js文件中找到导入示例 请调整它,使代码保存在js文件中 您好,请帮助在js文件中找到导入示例。请调整它,以便代码保存在js文件中

桌子
函数myim(){
a、 innerHTML=在b.js中导入代码
}


您可以在javascript中使用
XMLHttpRequest
从服务器请求数据。
file.open(“GET”,“b.js”)可用于打开文件。
file.responseText
用于从该文件获取数据

注意:

在web服务器(本地或在线)上使用此代码可以避免某些浏览器中的CORS策略。

桌子
函数myim(){
var file=new XMLHttpRequest();
打开(“GET”、“b.js”);
file.onreadystatechange=函数(){
content=file.responseText;
document.getElementById('a').innerHTML=content;
}
file.send(空);
}


谢谢

您可能想研究如何使用Ajax请阅读.charlietfl文件中的导入代码这不是代码,这是HTML。不清楚您要做什么。您需要请求文件并对其内容进行处理……这就是ajax的用途。html代码应该在html文件中,而不是js文件中
<html dir="rtl">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>table</title>
</head>
<script>
  function myim() {
    var file = new XMLHttpRequest();
    file.open("GET", "b.js");
    file.onreadystatechange = function (){
        content = file.responseText;
        document.getElementById('a').innerHTML = content;
    }
    file.send(null);
  }
</script>

<body >
    <div id="a" align="center"></div>

    <p><input type="button" name="go" id="bn" value="import" onclick="myim()"></p>
</body>

</html>