将 HTML 文件导入本地主机上的 JS 文件

问题描述 投票:0回答:0

我目前正在为 Scratch 做一个扩展。为此,我必须在本地主机上运行它。现在,扩展的主要文件是 index.js,在该文件中,当我尝试导入其他 js 文件时它工作正常,但是当我尝试导入 HTML 文件时出现问题。我的代码片段版本是这样的:

div = document.createElement('div');
var btn = document.createElement('button');
btn.textContent = 'Close';
btn.onclick = function(){
    div.remove();
}
btn.style.margin = '10px';
var content = document.createElement('div');
content.style.width = '100%';
content.style.height = '100%';
content.style.backgroundColor = 'white';
var iframe = document.createElement('iframe');
// iframe.src = './node_modules/scratch-vm/src/extensions/scratch3_tsp/new.htm';

const path = require('path');
const filePath = path.join(__dirname, 'node_modules', 'scratch-vm', 'src', 'extensions', 'scratch3_tsp', 'new.htm');
iframe.src = filePath;

content.appendChild(iframe);
div.id = 'tsp_viz';
div.style.position = 'absolute';
div.style.width = '500px';
div.style.height = '500px';
div.style.zIndex = '1000';
div.style.backgroundColor = 'red';
div.style.top = '10%';
div.style.left = '30%';
div.style.borderRadius = '10px';
div.appendChild(btn);
div.appendChild(content);

文件目录为:“C:\Users\Ubaid ul Akbar\Desktop\Research\Development\scratch-gui ode_modules\scratch-vm\src xtensions\scratch3_tsp ew.htm”。该文件只包含一些虚拟文本,没有什么特别的。 我在“scratch-gui”目录中运行 Node Js。我尝试将文件重命名为 new.html 和 new.htm,我是否遗漏了什么?

javascript html node.js localhost
© www.soinside.com 2019 - 2024. All rights reserved.