Javascript window.onload not firing

Javascript window.onload not firing

本文关键字:firing not onload window Javascript      更新时间:2023-09-26

我正在尝试编写一个JavaScript函数,该函数强制元素具有样式显示=块。目前,它被设置为显示=无被另一个javascript。

我认为window.onload可以解决问题,因为代码不会被以前的JS覆盖。下面的代码曾经工作过,现在函数甚至没有触发。

谁能帮忙?

          window.onload =  function ()   // It sets the class of the unordered list when the page has been loaded.
            {
               ul.style.display = "block!important";   // Display all the child items
             }
您需要

在事件侦听器的回调中获取ul,因为如果您之前这样做,DOM 不会完全加载。

document.addEventListener("DOMContentLoaded", function(event) { 
    var ul = document.getElementById("my-ul");
    ul.style.display = "block!important";
});

如果你想要一个更跨浏览器的解决方案,你可以用JQuery来做到这一点:

$(document).ready(function() {
    $('#my-ul').css('display', 'block!important');
});

你可以在 css 中做到这一点:

#my-ul {
    display: block!important;
}

您的ul应采用以下解决方案的工作方式:

<ul id="my-ul">
你不能

使用这样的ul.style.display

使用document. getElementsByTagName('ul').style.display = "block !important"

window.onload = function() ... 正在定义一个函数。只需在使用 window.onload() 关闭它后立即调用该函数; 几个小时后为我工作