I'm a beginner.
I have stumbled on the conditional branch of javascript, so I would like to ask you a question.
What I want to do is to prepare multiple elements and click on each element to flag them and do the following:
Specifically,
Click on element 1 → "If you scroll down, you will be redirected to link a" can be performed.
Click on element 2 → "Scroll up and redirect to link b" processing becomes executable.
Click on element 3 → "Scroll Disabled" action is now available.
In a state where each element is clicked, the processing of the other element is turned into an invalid state.
That's what it looks like.
vara;
$("Element 1").on('click', function(){
a = 1;
});
$("Element 2").on('click', function(){
a = 2;
});
$("Element 3").on('click', function(){
a = 3;
});
Click on the element, then swing each number to variable a, and
switch(a){
case1:
Redirect to link a after scrolling down
break;
case2:
The action of being redirected to link b after scrolling up.
break;
case3:
Disable Scroll
break;
}
I wrote this conditional branch, but it didn't work.
When you click on an element, I have confirmed that each number is included in a, but the conditional branch statement is not executed.
Does the conditional branch itself not run?(Alert or console.log written in the switch statement was not executed either.)
I used to write conditional branches in if sentences, but it didn't work and I got stuck.
Please give me some advice.
javascript
function switchFunction(a){ switch(a){ case1: Redirect to link a after scrolling down break; case2: The action of being redirected to link b after scrolling up. break; case3: Disable Scroll break; } } $("Element 1").on('click', function(){ switchFunction(1); }); $("Element 2").on('click', function(){ switchFunction(2); }); $("Element 3").on('click', function(){ switchFunction(3); });
Try changing it as shown in .
© 2024 OneMinuteCode. All rights reserved.