内容显示中的 JavaScript 错误“无法设置未定义的属性”显示”

Javascript error in content display " Cannot set property "display" of undefined "

本文关键字:显示 设置 属性 未定义 错误 JavaScript      更新时间:2023-09-26

我在使用某些javascript时遇到了问题,我找到了作为内容切换的解决方案。

在网页上:www.enyx.sk/cubecraft,当您加载页面时,您应该获得第一个故事或选项卡"O nas"的内容,但在页面加载后,此页面的内容不显示,您必须单击菜单链接以显示...

我在网站加载后检查了代码并收到以下错误:未捕获的类型错误:无法设置未定义的属性"显示"。

需要明确的是,我是JS的新手,这是我在网上找到的内容切换解决方案。到目前为止它工作正常,我无法弄清楚是什么原因导致第一个内容隐藏......

非常感谢任何帮助

下面的代码片段中存在一个问题。 因为 firstChild 不会返回您期望的div。

var firstone=document.getElementById('stories').firstChild;
if (firstone.nodeType != 1) {firstone = firstone.nextSibling;}
firstone.style.display="block";
} 

与其尝试使用它,不如尝试使用它。

document.getElementById('stories').getElementsByClassName("story")[0].style.display= 'block'

它会工作:-(

编辑

var firstone=document.getElementById('stories').getElementsByClassName("story")[0]
    if (firstone.nodeType != 1) {firstone = firstone.nextSibling;}
    firstone.style.display="block";