About the question that onclick clicks on the main points of the event twice before it can be displayed.

menu [4] .onclick = function () {

dis ("menu4");

}

function dis (disp) {

var str=document.getElementById (disp);
var dis=str.style.display;
if (dis=="none") {
str.style.display="block";
} else {
str.style.display="none";

}

}
the effect of this simple drop-down menu needs to be clicked twice to realize why and how to deal with it

Mar.20,2021

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div id="btn" style="width: 100px;background-color: yellow;"></div>
    <div id="disp" style="width:400px;height: 400px;background-color: red;"></div>
    <script>
        var Btn = document.getElementById('btn');
        Btn.onclick = function(){
            dis('disp');
        };

        function dis(getId){
            var str = document.getElementById(getId);
            var dis = str.style.display;
            if( dis == 'none' ){
                str.style.display = 'block';
            }else{
                str.style.display = 'none';
            }
        }
    </script>
</body>
</html>

I don't see what's wrong with your code. If you want to know bug, you'll have to use browser development mode to break the point.


display has not been initialized


var dis = str.style.display; to see whether the dis at this time is block or none, if it is block clicked for the first time to determine whether it is str.style.display='none'; or hidden; click again to show block.


first of all, brother, shouldn't you put quotation marks when you get his ID name? Not strictly speaking, I don't know why you executed this. You called another function in the click event. Actually, brother, if you only want to click once, wouldn't it be better for you to write that to the DOM0 event? Or if you want to click twice, why not tie a dbonclick? The variables you declare inside the function body are all local variables. Aren't you worried that you won't be able to reference them when you want to refer to them externally?

Menu