单击时 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
jQuery change variable on click?
提问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
回答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 测试 -
With your Html revision it works too -
使用您的 Html 修订版,它也可以工作 -
Are you sure the lang variable is within function scope?
您确定 lang 变量在函数范围内吗?