简体   繁体   English

在子局部函数javascript中更改全局变量的值

[英]Changing value of global variable in sub local function javascript

There is no need the explain most of this code. 无需解释大部分代码。 As you can see the global variable cancel is set to true. 如您所见,全局变量cancel设置为true。 Then a function is called in which the variable cancel is changed. 然后调用一个函数,其中更改变量cancel It seems as though I can access the cancel variable on lines 15, 27, or 32. Can you explain why it doesn't work and how I can fix it? 似乎我可以在第cancel或32行上访问cancel变量。 您能解释一下为什么它不起作用以及如何解决它吗? Thank you! 谢谢! Please ask any additional questions. 请询问其他任何问题。 I didn't explain the other code here because I don't think it pertains to the question directly and it would make this question too long to read if I were. 我在这里没有解释其他代码,因为我认为它与问题不直接相关,如果我这样做的话,这个问题将使阅读时间太长。

1. var cancel = 'true';
2. function setSliders()
3. {
4.  var sliders = document.getElementsByClassName('slider');
5.  var sliderButtons = document.getElementsByClassName('sliderButton');
6.  for(var i = 0; i != sliderButtons.length; i++)
7.  {
8.      var slider = document.getElementsByClassName('sliderButton')[i];
9.      slider.onmousedown = function()
10.     {
11.         cancel = 'false';
12.         this.onmouseup = function(cancel)
13.         {
14.             cancel = 'true';
15.             alert(cancel +' within semi function cancel does not seem to be accessible here');
16.             //alert('test');
17.         };
18.         
19.         alert(cancel+' within function');
20.         
21.         this.onmousemove = function(event, cancel)
22.         {
23.             if(cancel == 'false')
24.             {
25.                 console.log('cancel is false'); //cancel doesn't seem to be accessible here either.
26.                 this.style.left = event.clientX+'px';
27.                 cancel = 'true';
28.             }
29.             else
30.             {
31.                 console.log('cancel is true'); //cancel isn't accessible here either.
32.                 cancel = 'false';
33.             }
34.         };
35.     };

You have cancel as argument name to your functions, making cancel within those functions be local to that function. 您已将cancel作为函数的参数名称,使这些函数内的cancel对于该函数而言是本地的。

If you remove cancel from the arguments, it will reference the global cancel variable and should work. 如果从参数中删除cancel ,它将引用全局cancel变量,并且应该起作用。

 1. var cancel = 'true'; 2. function setSliders() 3. { 4. var sliders = document.getElementsByClassName('slider'); 5. var sliderButtons = document.getElementsByClassName('sliderButton'); 6. for(var i = 0; i != sliderButtons.length; i++) 7. { 8. var slider = document.getElementsByClassName('sliderButton')[i]; 9. slider.onmousedown = function() 10. { 11. cancel = 'false'; 12. this.onmouseup = function() 13. { 14. cancel = 'true'; 15. alert(cancel +' within semi function cancel does not seem to be accessible here'); 16. //alert('test'); 17. }; 18. 19. alert(cancel+' within function'); 20. 21. this.onmousemove = function(event) 22. { 23. if(cancel == 'false') 24. { 25. console.log('cancel is false'); //cancel doesn't seem to be accessible here either. 26. this.style.left = event.clientX+'px'; 27. cancel = 'true'; 28. } 29. else 30. { 31. console.log('cancel is true'); //cancel isn't accessible here either. 32. cancel = 'false'; 33. } 34. }; 35. }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM