简体   繁体   English

当存在具有该 ID 的元素时,为什么 document.getElementById() 返回 null?

[英]Why is document.getElementById() returning null when there is an element with that Id?

I'm trying to build a navbar in React that has a child component Menu that renders one of two different versions of the menu depending on the width on the div that the Menu is rendered inside.我正在尝试在 React 中构建一个导航栏,该导航栏有一个子组件Menu ,它根据菜单在其中呈现的 div 上的宽度呈现两个不同版本的菜单之一。 There is also a logo and company name that take up a relatively constant amount of width in the navbar and the div that holds the menu grows and shrinks to adjust for screen width and aspect ratio changes.还有一个徽标和公司名称在导航栏中占据相对恒定的宽度,并且保存菜单的 div 会随着屏幕宽度和纵横比的变化而增长和缩小。 Anyway, that div that grows and shrinks I gave an id="menu-section" so that I could use document.getElementById("menu-section").offsetWidth;无论如何,那个增长和缩小的 div 我给了一个id="menu-section"以便我可以使用document.getElementById("menu-section").offsetWidth; to grab the available width and use that in the function that decided which version of the menu to render.获取可用宽度并在决定渲染哪个版本的菜单的 function 中使用它。 The trouble is I get a TypeError: Cannot read property 'offsetWidth' of null .问题是我得到一个TypeError: Cannot read property 'offsetWidth' of null Why would the document.getElementById("menu-section") be failing to find the div and returning null?为什么document.getElementById("menu-section")找不到 div 并返回 null?

function Menu(props) {
    let availableWidthPx = document.getElementById("menu-section").offsetWidth;
    if (availableWidthPx > 600) {
        return expanded version;
    }
    return collapsed version
}

function Navbar(props) {
    render(){
        return (
            <nav className="navbar">
                ...
                <div id="menu-section">
                    <Menu />
                </div> 
            </nav>
        )
    }
}
export default Navbar;

Because the DOM is not loaded yet.因为 DOM 尚未加载。 Here's how to fix it:以下是解决方法:

function Menu(props) {
  React.useEffect(() => {
    let availableWidthPx = document.getElementById("menu-section");
    if (availableWidthPx) {
      console.log(availableWidthPx.offsetWidth);
    }

  }, [])

 return <div>Hello</div>;
}

Also, your Navbar functional component needs just return , not render此外,您的Navbar功能组件只需要return ,而不是render

Because there isn't an element with that ID until all the rendering has finished and the result added to the DOM … but you are trying to call it from the render method.因为在所有渲染完成并将结果添加到 DOM 之前,没有具有该 ID 的元素……但是您正试图从 render 方法中调用它。

You should also consider calling the Menu function after rendering, or define it in an afterRender() block.您还应该考虑在渲染后调用菜单 function,或者在afterRender()块中定义它。

Function Navbar doesn't add anything to the DOM. Navbar导航栏不会向 DOM 添加任何内容。

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

相关问题 当元素是空文本框时,Javascript document.getElementById(&quot;id&quot;).value 返回 null 而不是空字符串 - Javascript document.getElementById("id").value returning null instead of empty string when the element is an empty text box 当我知道ID存在时,为什么document.getElementById()返回null值? - Why is document.getElementById() returning a null value when I know the ID exists? 为什么document.getElementById返回空值? - Why is document.getElementById returning a null value? document.getElementById返回null - document.getElementById is returning null document.getElementById(&#39;ID&#39;)是否仅在我的部分代码中返回null - document.getElementById('ID') is it returning null in only part of my code 无法使用document.getElementById获取元素,返回null - not able to get an element using the document.getElementById, returning null 对于已知元素,document.getElementById返回null - 为什么? - document.getElementById coming back with null for a known element - why? 搜索div ID时Document.getelementbyId返回null - Document.getelementbyId returns null when searching for div id 当element是表单的一部分时,document.getElementById返回null - document.getElementById returns null when element is part of a form document.getElementById在单击按钮时返回null - document.getElementById is returning null on the button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM