( Uncaught TypeError: Cannot set property 'display' of undefined

( Uncaught TypeError: Cannot set property 'display' of undefined

function open() {
document.getElementsByClassName("nav").style.display='flex';}

我试了几次显示项目都解决不了,希望有人 提前致谢!!

document.getElementsByClassName("nav")

The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node. You may also call getElementsByClassName() on any element; it will return only elements which are descendants of the specified root element with the given class name(s).

您可能需要执行以下操作(假设您只有一个元素 class name as nave):

(document.getElementsByClassName("nav")[0]).style.display='flex';