js解决:Uncaught TypeError: Cannot read property 'dsiplay' of undefined问题

As tens of thousands of javascript scholar, I encountered the same problem: Uncaught TypeError: Can not read property 'display' of undefined

But my situation is more special, then check online solutions to the same problem, and I find that I do not have a similar, we can not solve my problem.

Yes, I can only debug one, delete deletion change, finally found the answer.

 

1. I define the style of nodes using a class, with getElementsByClassName to get

 var turn = document.getElementsByClassName("turn");

2. But the class is a class set of elements is an array, returns all elements of class, then modify the style of the time I still use obj.style, there have been cases not read property

picture.style.display = "none"

3. Solution: replace the class with id is the simplest modifications

Html tags into the node id, css, js file the appropriate changes, you can use getElementById to get property

var turn = document.getElementById("turn");

summary:

getElementByClassName

This method does not return a specified element but a kind of element, namely the elements class names of all class set for you, so it returns the result should be an array, then you need to change the style from the array to take elements, and then for the element to set attributes, rather than directly set the properties array, so naturally invalid.

Usage (getElement s ByClassName Do not miss the s, behind the use of the specified array elements [0]):

1 

document.getElementsByClassName("bg")[0]

The following methods are explained;

the getElementById () method returns the element with the specified ID is: [return element]

getElementsByTagName () returns all elements with the specified tag name. [Return a collection of elements that the array]

getElementsByClassName () method

 

The final conclusion: This Summary section is reproduced, invasion deleted. Original https://www.imooc.com/qadetail/230436

Released three original articles · won praise 0 · Views 56

Guess you like

Origin blog.csdn.net/JiGewusuoweiju/article/details/104903809