简体   繁体   English

包含函数参数的Javascript变量名称

[英]Javascript variable name containing function parameter

I've been searching a bit for this problem, found some track about ways to solve it, using eval() and window[] but couldn't figure the good syntax out. 我一直在寻找这个问题,使用eval()和window []找到了一些解决问题的方法,但是找不到好的语法。

Here is my code : 这是我的代码:

<script>

var colored = false;

function color(object) {

    if(colored) {
        document.getElementById(object).style = "fill:#0000ff";
        colored = false;

    } else {
        document.getElementById(object).style = "fill:#000000";
        colored = true;
    }

}

</script>

This code is about coloring a svg path (representing an eye) by clicking on a link and then coming back to the original color by clicking again on the same link. 这段代码是关于通过单击链接为svg路径着色(代表一只眼睛),然后通过再次单击同一链接将其恢复为原始颜色。

<a id="o1-right" onclick="color('right-orbit')" href="#">

This code works fine for coloring a single element at a time. 这段代码可以一次为单个元素着色。

But if I want to use it for a second link, for example : 但是,如果我想将其用于第二个链接,例如:

<a id="o1-left" onclick="color('left-orbit')" href="#">

Then the variable named colored will mess up cause used two times. 然后,名为colored的变量将造成两次混乱。

I click on the right eye : Colored is assigned to false. 我单击右眼:将彩色赋给false。 The right eye is colored in black. 右眼为黑色。 Colored will get true. 有色会成真。 I click on the left eye : Colored is currently assigned to true. 我单击左眼:当前将彩色赋值为true。 The left eye will get blue. 左眼变蓝。 Colored will get false. 彩色会假。

I would like the left eye to turn black instead. 我想让左眼变黑。

The problem is coming from the variable which is used by the left eye link and the right eye link. 问题出在左眼链接和右眼链接使用的变量上。

I'm looking for a solution to dynamically name the variable depending of the object. 我正在寻找一种根据对象动态命名变量的解决方案。 In my head this would look like something like this : 在我的脑海中,这看起来像这样:

<script>

function color(object) {

var colored + object;

    if(colored + object) {
        document.getElementById(object).style = "fill:#0000ff";
        colored + object = false;

    } else {
        document.getElementById(object).style = "fill:#000000";
        colored + object = true;
    }

}

</script>

But I know this is completely wrong cause the syntax seems to be horrible and in each case the declaration var colored + object; 但是我知道这是完全错误的,因为语法似乎很糟糕,并且在每种情况下都声明var color + object; will mess up with the actual value of the variable :( 会弄乱变量的实际值:(

I don't even know if I'm not considering something really easy and obvious or if there is a real trick behind it. 我什至不知道我是否没有考虑真正简单和明显的事情,或者背后是否有真正的窍门。

Thank you for you help ! 谢谢你的帮助 !

Instead of having that global variable holding the colored state, consider toggling a class 'colored'. 与其让全局变量保持有色状态,不如切换为“有色”类。

You can do everything with css. 您可以使用CSS进行所有操作。

If you really have to do it in JavaScript, then you can check if your current object has the class 'colored' and then proceed according to that. 如果确实需要使用JavaScript进行操作,则可以检查当前对象是否具有“彩色”类,然后根据该类进行操作。

You can also consider adding a custom class to your < a > : 您还可以考虑将自定义类添加到<a>:

a.color-changeable.colored{fill:#000} a.color-changeable {fill:#0000ff}

Then to handle the click: 然后处理点击:

$('.color-changeable').click(function(){ $(this).toggleClass('colored'); });

The way I would solve this issue is by not using a global boolean to store whether or not the color has been adjusted (as this will only allow you to use the function for one element at a time). 我要解决此问题的方法是不使用全局布尔值来存储是否已调整颜色(因为这将仅允许您一次将函数用于一个元素)。

I would store the state of colored (true/false) on the element itself. 我将在元素本身上存储有色(true / false)状态。

Something like: 就像是:

function color(id) {
    var element = document.querySelector(id);
    if(element.colored) {
        document.getElementById(id).style = "fill:#0000ff";
        document.getElementById(id).dataset.colored = false;

    } else {
        document.getElementById(id).style = "fill:#000000";
        document.getElementById(id).dataset.colored = true;
    }

}

The above code is untested but should help you in the right direction. 上面的代码未经测试,但是应该在正确的方向上为您提供帮助。

More information on HTMLElement.dataset 有关HTMLElement.dataset的更多信息

In your case, I would make colored an object so that you can store multiple values in it and look them up by name: 在您的情况下,我将为一个对象colored ,以便您可以在其中存储多个值并按名称查找它们:

<script>

var colored = {}; // new object

function color(object) {

    if(colored[object]) {
        document.getElementById(object).style = "fill:#0000ff";
        colored[object] = false;

    } else {
        document.getElementById(object).style = "fill:#000000";
        colored[object] = true;
    }

}

</script>

You mention in your post that you had looked at a way to do make variables using window . 您在帖子中提到,您已经找到了一种使用window进行变量制作的方法。 The syntax for that would be like 语法就像

window["colored"+object] = false;

which you will notice is very similar to the syntax I used when I made colored an object. 您会注意到,这与为对象colored时使用的语法非常相似。 This is because global variables in JavaScript are also properties on the window object . 这是因为JavaScript中的全局变量也是window对象的属性

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

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