本文介绍了动态变量名语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
另一个新手问题使我陷入困境.
Another newbie question that has me stuck badly.
我有三个变量
var state1 var state2 var state3我需要在.click事件中访问其中之一,具体取决于被单击对象的属性.
I need to access one of them on a .click event depending on a property of the clicked object.
如何访问正确的变量?基本上,我需要通过将状态"和正确的数字加在一起来获得变量名.
How do I access the correct variable? Basically I need to for the variable name by adding "state" and the correct number together.
如果有需要,我会尽快通过一些代码更新此帖子.
I will update this post soon with some code in case it's needed.
var backstate1; var backstate2; var backstate3; var currentFrame; function hotspotHover(){ var currentElement = this; var daName =this.name; $("#hotspot" +daName).hover( function() { $("img.changeOutline").prop("src", currentElement.outlinesArray[currentFrame]); $("#outlines").show(); }, function() { $("#outlines").hide(); } ).click(function() { var backstateLevelNumber = currentElement.backStateLevel; //not working window['backstate'+ backstateLevelNumber]=currentFrame; //trying to set backstate+number to current frame variable hoverClick(currentElement.defaultFrame); checkReverse(); checkSeqLoop(); }); };推荐答案
使用数组可以解决您的问题:
Using an array would solve your problem:
var states = [null, null, null];访问它们的方式将是:(假设currentStateId是当前状态号)
Accessing them would then be: (Assuming currentStateId being the current state number)
var currentState = state[currentStateId] //Access state并进行设置:
states[currentStateId] = "State"; //Set State更多推荐
动态变量名语法
发布评论