// Gets all elements with className 1 given in quotes and takes the first element of that list by using [0]
const divOne = document.getElementsByClassName("1")[0];
changeDisplayOnElement(divOne);
//Similarly
const divTwo = document.getElementsByClassName("2")[0];
changeDisplayOnElement(divTwo);
//change the property display under style to none, if it's block
function changeDisplayOnElement(element){
element.style.display = element.style.display === "none" ? "block" : "none";
}
Use this snippet.
// Gets all elements with className 1 given in quotes and takes the first element of that list by using [0]
const divOne = document.getElementsByClassName("1")[0];
changeDisplayOnElement(divOne);
//Similarly
const divTwo = document.getElementsByClassName("2")[0];
changeDisplayOnElement(divTwo);
//change the property display under style to none, if it's block
function changeDisplayOnElement(element){
element.style.display = element.style.display === "none" ? "block" : "none";
}
发布评论
评论(1)
使用这个片段。
Use this snippet.