简体   繁体   English

我需要帮助来放置函数,以便在呈现DOM之后运行我的代码

[英]I need help to place a function so my code runs after the DOM has been rendered

I have a line of code that runs faster than the DOM so I need a timeout-function but I don't know where to put it. 我有一行代码比DOM运行得更快,因此我需要一个超时功能,但我不知道将其放在哪里。

function testing() {
  var newHeadline = document.createElement("h1");
  newHeadline.textContent = "Headline";

  document.getElementsByClassName("col-sm")[0].insertBefore(newHeadline, document.getElementsByClassName("text-content")[0]);
  for (var a = 0; a < 10; a++) {
    if (document.getElementsByClassName("form-control")[0] != undefined) {
      document.getElementsByClassName("form-control")[0].parentElement.remove();


      return true
    }
  }
}

Use window.onload. 使用window.onload。 window.onload ensures that first everything in the window including scripts and images have been loaded, and after everything is loaded it call the function which is supplied. window.onload确保首先加载窗口中的所有内容,包括脚本和图像,然后加载所有内容,然后调用提供的函数。

 window.onload=function testing() { var newHeadline = document.createElement("h1"); newHeadline.textContent = "Headline"; document.getElementsByClassName("col-sm")[0].insertBefore(newHeadline, document.getElementsByClassName("text-content")[0]); for (var a = 0; a < 10; a++) { if (document.getElementsByClassName("form-control")[0] != undefined) { document.getElementsByClassName("form-control")[0].parentElement.remove(); return true } } } 

Use the DOMContentLoaded event if you want to wait until all HTML has been loaded and JavaScript has been parsed, or the load event if you also want to wait until all the images and styles have been applied. 如果要等待直到所有HTML都已加载并且解析了JavaScript,请使用DOMContentLoaded事件;如果也要等待所有图像和样式都已应用,请使用load事件。

if (document.readyState === "complete") testing();
else addEventListener("DOMContentLoaded", testing);

OR 要么

if (document.readyState === "complete") testing();
else addEventListener("load", testing);

You can also use an empty timeout , to run the function right after the script has been parsed and run. 您还可以使用一个空的timeout ,在脚本被解析并运行之后立即运行该函数。

setTimeout(testing, 0);

暂无
暂无

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

相关问题 如何确保在渲染DOM之后调用我的dom操作函数/代码? - How to make sure my dom manipulating function/code is called after the DOM has been rendered? 即使已呈现 DOM,HTML 集合长度仍为 0 - HTML Collection length is 0 even after DOM has been rendered 呈现模板后,如何在空格键中更改模板助手功能? - How do I change template helper function in Spacebars after template has been rendered? 所以我需要将此代码提交给我的老师。 他有一个隐藏列表供我处理。 虽然它不起作用 - So i need to submit this code to my teacher. He has a hidden list for my function to work off. Its not working though 呈现出车手帮手后如何执行功能 - How to execute a function after a handlebars helper has been rendered 我需要修复 setInterval 以便它每个间隔只运行一次我的代码 - 目前运行太多次 - I need to fix setInterval so that it only runs my code once every interval - currently runs too many times 我需要帮助使JQuery Function slideDown在我的代码中工作 - I need help getting the JQuery Function slideDown to work in my code 将元素插入DOM后,如何对元素执行jQuery函数? - How do I perform a jQuery function on an element after it has been inserted into the DOM? 在DOM上单击按钮后如何调用函数 - How do i call a function after a button has been clicked on DOM 在 DOM 中呈现表格后,AngularJS Linkify 单元格与表格中的链接 - AngularJS Linkify cells with links in a table after table has been rendered in the DOM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM