简体   繁体   中英

Jquery onclick var update

This is a very simple, probably too simple question but I'm afraid my limited skills in jQuery require someone's help! Behold the shameful line of code:

onclick="javascript:  $j('#'+openc).fadeOut('normal', function({
$j('#'+openc).fadeIn('slow')});
openc=+1;
alert('The city of ' + openc + ' is located in ' + openc + '.');"

By default, openc is set to 0.

Onclick, the div with the id 0 is faded out, and I wish to have div id 1 open, then openc to b updated to 1. I added the alert for my own sake. When I press once, it seems to work. When I press it again, it still alerts 1, thus my div id 1 fades out and right back in. I think I just need to add +1 to the fadeIn function with the onclick, so that it will always open openc+1 but..can't seem to get the syntax right.

Thanks!

EDIT: Perhaps my question was not clear enough, I figured I would add some info. I beg you to not suggest things such as adding event-handlers, I have no clue what that even means, I am a total noob at javascript/jQuery. All I need to do is this:

onclick, fadeout the div openc (which yes, is a number) ,fadein openc +1, and update openc to its own value +1.

This is for a pagination of sorts between divs. So say you land on div id=0, openc=0. You click on next, it should fadeout div0, fadein div1, and openc should now equal1. That way, the next time you click next, div1 fades out, and div2 appears, etc.

Hope that clarifies, thanks again!

You should put this code in to an event handler, rather than use the outdated onclick HTML attribute. Try putting this in to the <head> of your page:

<script type="text/javascript">
    $(function() {
        $j("#myElement").click(function() {
            $j('#' + openc).fadeOut('normal', function() {
                $j('#' + openc).fadeIn('slow')
            });

            openc += 1;

            alert('The city of ' + openc + ' is located in ' + openc + '.');
        });
    });
</script>

Also, as stated in the comments Ids cannot start with numbers, so it'd be best to change those too.

openc=+1 makes it look line "openc" is a number. IDs cannot be numbers.

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