简体   繁体   中英

How to mix/add Javascript code to Html code on Visual studio code?

I have some problem to understand why wont my onclick="showMenu" and onclick="hideMenu" function work. Any recommendation?

<section class="header">

        <nav>
            <a href="index.html"><img src="Bilder/Logo.jpg"></a>
            <div class="nav-links" id="navLinks">
                <i class="fa fa-times"> <script>onclick="hideMenu()"</script></i>
                    <ul>
                        <li><a href="">HOME</a></li>
                        <li><a href="">ABOUT</a></li>
                        <li><a href="">COURSE</a></li>
                        <li><a href="">BLOG</a></li>
                        <li><a href="">CONTACT</a></li>
                    </ul>
            </div>
            <i class="fa fa-bars"><script>onclick="showMenu()"</script></i>
        </nav>

     <div class="text-box">
         <h1>Some text...</h1>
         <p>Some text..</p>
         <a href="" class="hero-btn">Button Text..</a>
     </div>   

    </section>

    <!-------Javascript for Menu--------->

<script>
        var navLinks = document.getElementById("navLinks");
        funnction showMenu(){
            navLinks.style.right = "0";
        }
        funnction hideMenu(){
            navLinks.style.right = "200px";
        }
</script>

在此处输入图像描述

I see two problems in your code.

  1. You are not passing the function, but rather the return value of the function.

  2. You are using "onClick" inside a script tag, which doesn't make anysense. Do you want to click the script?

Thats how it should look like:

<i class="fa fa-times" onclick="hideMenu"></i>

i suggested u to using a to click an icon

<nav>
    <a href="index.html">
        <img src="Bilder/Logo.jpg">
    </a>
    <div class="nav-links" id="navLinks">
        <a href="javascript:hideMenu()">
            <i class="fa fa-bars">
            </i>
        </a>
        <ul>
            <li><a href="">HOME</a></li>
            <li><a href="">ABOUT</a></li>
            <li><a href="">COURSE</a></li>
            <li><a href="">BLOG</a></li>
            <li><a href="">CONTACT</a></li>
        </ul>
    </div>
    <a href="javascript:showMenu()">
        <i class="fa fa-bars">
        </i>
    </a>

</nav>

<div class="text-box">
    <h1>Some text...</h1>
    <p>Some text..</p>
    <a href="" class="hero-btn">Button Text..</a>
</div>

</section>

<!-------Javascript for Menu--------->

<script>
    var navLinks = document.getElementById("navLinks");

    function showMenu() {
        navLinks.style.right = "0";
    }

    function hideMenu() {
        navLinks.style.right = "200px";
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM