动态变量名语法

编程入门 行业动态 更新时间:2024-10-10 10:25:50
本文介绍了动态变量名语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

另一个新手问题使我陷入困境.

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

更多推荐

动态变量名语法

本文发布于:2023-11-12 02:23:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   变量名   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!