jQuery change variable on click?

不想你离开。 提交于 2019-12-24 12:31:34

问题


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';
    });
}); 


来源:https://stackoverflow.com/questions/7506317/jquery-change-variable-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!