It's fairly straightforward if you read your code in order.
- When you click the button, it runs the
modal1()
function.
- This function, among other things, adds a
btn.onclick
listener, waiting for a click.
That's it.
So now, you have to click a second time in order to trigger the listener you just created.
The new onclick
listener you create, for that matter, overrides the current onclick
on the button, and I'm not sure why you do this.
The simple solution is to put modal.style.display = "block"
directly in modal1()
. Why override the onclick
function?
function modal1() {
modal.style.display = "block";
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…