简体   繁体   中英

Switch statement in JavaScript function is not activating

I am trying to use a switch statement in a function, but it is not activating.

 let thisFunction = function(index) { console.log(index); switch (index) { case 0: console.log('Home'); break; case 1: console.log('About'); break; case 2: console.log('Services'); break; case 3: console.log('Portfolio'); break; case 4: console.log('Contact'); break; default: console.log('Default'); } }; $('nav ul li a').click(function() { let index = $(this).attr('data-btnIndex'); thisFunction(index); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <ul> <li> <a href="#" data-btnIndex="0">Home</a> </li> <li> <a href="#" data-btnIndex="1">About Us</a> </li> <li> <a href="#" data-btnIndex="2">Services</a> </li> <li> <a href="#" data-btnIndex="3">Portfolio</a> </li> <li> <a href="#" data-btnIndex="4">Contact</a> </li> </ul> </nav> 

When I run the code above and click a link, the console.log of "index" runs but the switch block does nothing.

If I define the index variable like in the example below, the switch works. But when I put the same code in the function, the switch stops working.

Example:

let index = 3;

console.log(index);
switch(index){
    case 0:
        console.log('Home');
    break;

    case 1:
        console.log('About');
    break;

    case 2:
        console.log('Services');
    break;

    case 3:
        console.log('Portfolio');
    break;

    case 4:
        console.log('Contact');
    break;

    default:
        console.log('Default');
}

This example returns "3" and "Portfolio" in the console, as it should.

.attr returns a string . (Your manual snippet is defining index as a number , which is why its behavior is different) Either test case s against numeric strings like '0' , or cast the index to a number first:

 let thisFunction = function(index) { console.log(index); switch (index) { case 0: console.log('Home'); break; case 1: console.log('About'); break; case 2: console.log('Services'); break; case 3: console.log('Portfolio'); break; case 4: console.log('Contact'); break; default: console.log('Default'); } }; $('nav ul li a').click(function() { let index = $(this).attr('data-btnIndex'); console.log(typeof index); thisFunction(Number(index)); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <ul> <li> <a href="#" data-btnIndex="0">Home</a> </li> <li> <a href="#" data-btnIndex="1">About Us</a> </li> <li> <a href="#" data-btnIndex="2">Services</a> </li> <li> <a href="#" data-btnIndex="3">Portfolio</a> </li> <li> <a href="#" data-btnIndex="4">Contact</a> </li> </ul> </nav> 

<button> vs <a> . I recommend you stick with anchor for navigation bcs that's what it's made for. Re the dataset attributes you are using, they are not required. Simply use the innerHTML.toLowerCase() for your switch.

Regarding your issue, your page is navigating away before your script gets to handle the .click .

Simply add a CSS class to whatever you want in the page HTML anchor. In other words, in about.html have <a class="active-page">About</a> .

Your code is perfectly fine, but there is only one minor issue, that prevents the correct output. Your switch is not stopping, its running perfectly, but the type of the value of the attribute data-btnIndex is string. So instead of case 0 , you should write case '0' , instead of case 1 you should write case '1' and so on.

Or you you can parse the value of data-btnIndex to Integer by the following method:

var index = parseInt(index);

I hope this helps.

Thanks.

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