[英]Element inserted with innerHTML doesn't support scripts of the document
The scripts loaded in the document doesn't work on the injected code with innerHTML.文档中加载的脚本不适用于使用 innerHTML 注入的代码。 I tried to put async or defer but nothing happened.
我试图放置异步或延迟但没有任何反应。
...
<script src="../scripts/traffic-lights.js"></script>
<script src="../scripts/resizeable-windows.js"></script>
<script src="../scripts/draggable-windows.js"></script>
<script>
const app = document.getElementById('app');
app.addEventListener("click", function() {addWindow("./app.html", "AppName")});
function addWindow(location, name) {
const windows = document.createElement('section');
windows.id = "window";
windows.innerHTML = `
<div class="header" id="header">
<div class="traffic-lights" id="traffic-lights">
<button class="red" id="red"></button>
<button class="yellow" id="yellow"></button>
<button class="green" id="green"></button>
</div>
<div id="movable">
<div class="appname">${name}</div>
</div>
</div>
<iframe src=${location} width= "100%" height="100%" style="border:none;"></iframe>`
document.body.appendChild(windows);
}
</script>
Here the code for the script below:这是下面脚本的代码:
element = document.getElementById("window");
makeResizable(element, 400, 225, 10);
...
dragElement(document.getElementById("window"));
function dragElement(element) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById("movable")) {
// if present, the header is where you move the DIV from:
document.getElementById("movable").onmousedown = dragMouseDown;
} else {
// otherwise, move the DIV from anywhere inside the DIV:
element.onmousedown = dragMouseDown;
}
...
let trafficLights = document.getElementById("traffic-lights");
let redButton = document.getElementById("red");
let yellowButton = document.getElementById("yellow");
let greenButton = document.getElementById("green");
let windows = document.getElementById("window");
trafficLights.onmouseenter = function () { ...
In your first 3 scripts, you query Elements (with document.getElementById()
) which are not on the DOM yet (because they are added later dynamically by your addWindow
function).在您的前 3 个脚本中,您查询尚未在 DOM 上的元素(使用
document.getElementById()
)(因为它们稍后由您的addWindow
函数动态添加)。 Hence you actually get no Element.因此你实际上没有得到任何元素。
As suggested by DeeGee and Kooilnc in the question comments, one of the possible solutions would be to use event delegation, attached on an already existing parent Element (typically the document
, but could be app
in your case).正如 DeeGee 和 Kooilnc 在问题评论中所建议的那样,一种可能的解决方案是使用事件委托,附加在已经存在的父元素上(通常是
document
,但在您的情况下可能是app
)。
Another possible solution would be to initialize your 3 scripts only once your addWindow
function has inserted your Elements into the DOM:另一种可能的解决方案是仅在您的
addWindow
function 将您的元素插入 DOM 后才初始化您的 3 个脚本:
addWindow
after the DOM insertion:addWindow
中执行:// traffic-lights.js
// Wrap the setup code with a function
function setupTrafficLights() {
let trafficLights = document.getElementById("traffic-lights");
trafficLights.onmouseenter = function () {}
}
<!-- index.html -->
<script src="../scripts/traffic-lights.js"></script>
<script>
// ...
function addWindow() {
// ...
// Insert into DOM
document.body.appendChild(windows)
// Now we can execute setup code that queries injected Elements into DOM
setupTrafficLights()
}
</script>
<script>
tags.<script>
标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.