简体   繁体   English

使用 innerHTML 插入的元素不支持文档的脚本

[英]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:这是下面脚本的代码:

  • resizeable-windows.js resizeable-windows.js
element = document.getElementById("window");
makeResizable(element, 400, 225, 10);
...
  • draggable-windows.js可拖动的windows.js
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;
  }
...
  • traffic-lights.js交通灯.js
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 个脚本:

  • You can wrap their code in a function (1 per script file), that you execute inside addWindow after the DOM insertion:您可以将他们的代码包装在 function(每个脚本文件 1 个)中,在插入 DOM 后在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>
  • You could also dynamically inject the 3 <script> tags.您还可以动态注入 3 个<script>标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM