jQuery change variable on click?

2019-07-28 21:48发布

问题:

i am sure its a simple thing. But i cant find the solution. My Code:

var lang='de';

$('#en').click(function (){
    lang='en';
});

The variable dont change / updates on click, why?

Thanks!

Solution: works not local only on Webserver for me.

回答1:

I tried this and it seems to work fine for me. More than likely you have a scope issue.

jsFiddle



回答2:

Sure it does. Here is the jsFiddle test -

http://jsfiddle.net/RfDCU/

With your Html revision it works too -

http://jsfiddle.net/RfDCU/1/

Are you sure the lang variable is within function scope?



回答3:

Maybe you are missing the document.ready ?

$(function(){
var lang='de';
    $('#en').click(function (){
        lang='en';
    });
});