我的onclick代码在第一次双击时为什么有效?

huangapple go评论72阅读模式
英文:

Why is my onclick code working on double click during the first time?

问题

我正在为使用Nextjs构建的网站编写一个简单的弹出窗口。它使用了onclick属性。我只是获取id并检查CSS中的display属性是否为none,然后使用if else来相应地设置display为none或block。它运行正常,唯一的问题是当页面加载时,第一次需要点击两次。

以下是与此问题相关的Nextjs代码,它是有效的,我已经尝试过。如上所述,为了使其开始工作,需要点击两次。我希望它能在第一次点击时就能工作。
网站的URL - gurjotsinghdev.vercel.app
Github源代码 - https://github.com/gurjotsinghdev/gurjotsinghdev

let pokepop = () => {
    let pokepop = document.getElementById("pokepopboxid");
    if (pokepop.style.display == "none") {
        pokepop.style.display = "block";
    } else {
        pokepop.style.display = "none";
    }
}

export default function Header() {
    return (
        <>
            <div className={styles.header}>
                <h1 className={styles.logo}>
                    <Link href="/"><a>
                        <Image
                            src={logo}
                            alt="作者图片"
                            className={styles.logo}
                            width={80}
                            height={80}
                        />
                    </a></Link>
                </h1>
                <div className={styles.mainMenu}>
                    <Link href="/projects"><a>项目</a></Link>
                    <Link href="/about"><a>关于</a></Link>
                    <Link href="/blog"><a>博客</a></Link>
                    <Link href="/">
                        <a onClick={() => pokepop()}>
                            <Image
                                src={pokeball}
                                alt="作者图片"
                                className={styles.pokeball}
                                width={40}
                                height={40}
                            />
                        </a>
                    </Link>
                </div>
                <div id="pokepopboxid" className="pokepopbox">
                    <h2>我的宝可梦</h2>
                </div>
            </div>
        </>
    )
}
英文:

I am writing a simple popup for my website build with Nextjs. It uses the onclick attribute. I am simply taking the id and checking if display property in CSS is none using if else. Accordingly I am setting display to none or block. It works fine, the only problem is when the page loads, for the first time, it takes two clicks.

Following in the code in Nextjs concerned to this issue that is working and I have tried. As described, for it to start working it takes two clicks. I want it to work in the first click itself.
Url of the website - gurjotsinghdev.vercel.app
Github Source code - https://github.com/gurjotsinghdev/gurjotsinghdev

let pokepop = () =&gt; {
let pokepop = document.getElementById(&quot;pokepopboxid&quot;);
if (pokepop.style.display == &quot;none&quot;) {
pokepop.style.display = &quot;block&quot;;
} else {
pokepop.style.display = &quot;none&quot;;
}
}
export default function Header() {
return (
&lt;&gt;
&lt;div className={styles.header}&gt;
&lt;h1 className={styles.logo}&gt;
&lt;Link href=&quot;/&quot;&gt;&lt;a&gt;
&lt;Image
src={logo}
alt=&quot;Picture of the author&quot;
className={styles.logo}
width={80}
height={80}
/&gt;
&lt;/a&gt;&lt;/Link&gt;
&lt;/h1&gt;
&lt;div className={styles.mainMenu}&gt;
&lt;Link href=&quot;/projects&quot;&gt;&lt;a&gt;Projects&lt;/a&gt;&lt;/Link&gt;
&lt;Link href=&quot;/about&quot;&gt;&lt;a&gt;About&lt;/a&gt;&lt;/Link&gt;
&lt;Link href=&quot;/blog&quot;&gt;&lt;a&gt;Blog&lt;/a&gt;&lt;/Link&gt;
&lt;Link href=&quot;/&quot;&gt;
&lt;a onClick={() =&gt; pokepop()} &gt;
&lt;Image
src={pokeball}
alt=&quot;Picture of the author&quot;
className={styles.pokeball}
width={40}
height={40}
/&gt;&lt;/a&gt;&lt;/Link&gt;
&lt;/div&gt;
&lt;div id=&quot;pokepopboxid&quot; className=&quot;pokepopbox&quot;&gt;
&lt;h2&gt;My Pokemon&lt;/h2&gt;
&lt;/div&gt;
&lt;/div &gt;
&lt;/&gt;
)
}

答案1

得分: 0

let pokepop = () => {
    let pokepop = document.getElementById("pokepopboxid");
    if (pokepop.style.display == "block") {
        pokepop.style.display = "none";
    } else {
        pokepop.style.display = "block";
    }
}
英文:
let pokepop = () =&gt; {
let pokepop = document.getElementById(&quot;pokepopboxid&quot;);
// First click, pokepop.style.display = &quot;&quot;;
if (pokepop.style.display == &quot;none&quot;) {
pokepop.style.display = &quot;block&quot;;
} else {
pokepop.style.display = &quot;none&quot;;
}
}

pokepop.style.display is an empty string on startup because it hasn't been set to anything, and the style property doesn't read the CSS values already there. This means that, on the first click, it goes into the else statement and sets the display to &quot;none&quot;. Then, on the next click, the check pokepop.style.display == &quot;none&quot; passes because it was set to &quot;none&quot; the previous click.

A simple fix is to invert the check, check if pokepop.style.display == &quot;block&quot; and if so, set it to &quot;none&quot;. Otherwise, set it to &quot;block&quot;. This will cause the first click to set it to &quot;block&quot; rather than &quot;none&quot;.

let pokepop = () =&gt; {
let pokepop = document.getElementById(&quot;pokepopboxid&quot;);
if (pokepop.style.display == &quot;block&quot;) {
pokepop.style.display = &quot;none&quot;;
} else {
pokepop.style.display = &quot;block&quot;;
}
}

huangapple
  • 本文由 发表于 2023年1月9日 08:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定