单击时 jQuery 更改变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7506317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:56:26  来源:igfitidea点击:

jQuery change variable on click?

jqueryvariablesclick

提问by user715449

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.

解决方案:对我来说,不仅在 Web 服务器上本地工作。

采纳答案by Gabe

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

我试过这个,它似乎对我来说很好。您很可能有范围问题。

jsFiddle

js小提琴

回答by aziz punjani

Maybe you are missing the document.ready?

也许你错过了document.ready

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

回答by Kris Krause

Sure it does. Here is the jsFiddle test -

当然可以。这是 jsFiddle 测试 -

http://jsfiddle.net/RfDCU/

http://jsfiddle.net/RfDCU/

With your Html revision it works too -

使用您的 Html 修订版,它也可以工作 -

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

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

Are you sure the lang variable is within function scope?

您确定 lang 变量在函数范围内吗?