gpt4 book ai didi

将样式表添加到类后,Javascript 不会继续

转载 作者:太空狗 更新时间:2023-10-29 15:13:48 26 4
gpt4 key购买 nike

如果设备有特殊宽度,我想显示一个 div 容器。我做了一个简单的 if 条件,它运行良好。

我想在我的代码中添加第二个函数,但该函数未被调用。尝试后,我发现代码在 document.getElementyById-Code 之后停止。

如果 alert("Hello World") 在 if-condition before getElementById 中被调用,则它被调用。如果在 if 条件 after getElementById 中调用它,则不再调用它。

为什么进度卡在那里?

控制台输出:[错误] TypeError: null 不是一个对象(评估 'document.getElementById("whitecontainer").style') setHeightandWidth(-链接已删除-,第 402 行) onresize(-链接已删除-,第 382 行)

但是对象不能为空,因为样式更改确实有效,只是在调整大小后卡住了。

感谢您的帮助!

function setHeightandWidth() {
var body = document.body,
html = document.documentElement;

var height = (Math.max(body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight) - 75 - 248);

var width = document.body.clientWidth;

if (width < 1199) {
document.getElementById("whitecontainer").style.display = "none";

alert("Hello World!");

}

if (width >= 1199) {
width = 1140;
document.getElementById("whitecontainer").style.display = "inherit";
}

var white_container = document.getElementById("whitecontainer");
white_container.style.height = height + "px";
white_container.style.width = width + "px";

}

最佳答案

问题出在这一行

document.getElementById("whitecontainer").style.display

javascript 无法找到 id "whitecontainer"这就是它抛出空异常和脚本中断的原因

关于将样式表添加到类后,Javascript 不会继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30396933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com