简体   繁体   English

JavaScript getElementById如何在括号内获得创意?

[英]JavaScript getElementById how creative can you get inside the parenthesis?

I'm working on a little project to improve my js skills and I'm currently at a standstill. 我正在做一个小项目来提高自己的js技能,但目前处于停滞状态。

If you look at my code below (doesn't work) at getElementById, inside the parenthesis I'm trying to get the word size and combined it with the variable value (value contains the option value the user selected). 如果您在下面的getElementById上查看我的代码(不起作用),则在括号内,我试图获取单词大小并将其与变量值组合在一起(值包含用户选择的选项值)。 So for example it would be getElementById(size28[i]), but that 28 changes of course. 因此,例如它将是getElementById(size28 [i]),但那28个当然会发生变化。

So I guess my question is, is there anyway that I can do that? 所以我想我的问题是,无论如何我能做到吗?

var allSizes = new Array(28,30,32);
var size28 = new Array("tgp_0", "tgp_1", "tgp_2", "tgp_3");
var size30 = new Array("tgp_0", "tgp_1", "tgp_2");
var size32 = new Array("tgp_0", "tgp_1", "tgp_2");

var index = document.rightAngle.ddHP.selectedIndex;
var value = document.rightAngle.ddHP.options[index].value;

for (var i=0; i<allSizes.length; i++) {
    if (value == allSizes[i]) {
        var s = "size";
        for (var j=0; j<6; j++) {
            document.getElementById(s+value+[j]).style.display = 'block';
        }
    }
}

Then the HTML portion: 然后是HTML部分:

<select name="ddHP">
 <option value="28">28</option>
 <option value="30">30</option>
 <option value="32">32</option>
</select>

<div id="tgp_0" class="idiv"><h1>Item 0</h1></div>
<div id="tgp_1" class="idiv"><h1>Item 1</h1></div>  
<div id="tgp_2" class="idiv"><h1>Item 2</h1></div>
<div id="tgp_3" class="idiv"><h1>Item 3</h1></div>  

You can't reference a variable by its name as a string (well, you can using a thing called eval , or I suppose by doing window["nameOfVariable"] , but that's really messy and not recommendable at all). 您不能通过变量的名称将其作为字符串引用(嗯,可以使用名为eval的东西,或者我想通过执行window["nameOfVariable"] ,但这确实很混乱,根本不window["nameOfVariable"]推荐)。 I would recommend putting all your size arrays in another array, or better yet, an object literal. 我建议将所有大小数组放入另一个数组,或者更好的是将一个对象常量放入。

Like so: 像这样:

var sizes = {
  "28": new Array("tgp_0", "tgp_1", "tgp_2", "tgp_3"),
  "30": new Array("tgp_0", "tgp_1", "tgp_2"),
  "32": new Array("tgp_0", "tgp_1", "tgp_2")
};

Now, it's much easier to programmatically reference any particular size array. 现在,以编程方式引用任何特定大小的数组要容易得多。 Instead of having to get the exact variable named "size28", you can just refer to sizes["28"] (you can change those keys to be integers instead of strings if you prefer; doesn't matter really). 不必获取名为“ size28”的确切变量,您只需引用size sizes["28"] (如果您愿意,可以将这些键更改为整数而不是字符串;这没关系)。

So now, you just check if value exists in the sizes object, and perform your CSS-modifying operation if it does exist: 因此,现在,您只需检查sizes对象中是否存在value ,然后执行CSS修改操作(如果确实存在):

var index = document.rightAngle.ddHP.selectedIndex;
var value = document.rightAngle.ddHP.options[index].value;

// check if this value exists in the sizes array
// (edit: actually, this check is kind of redundant since you know the <select> tag only contains options "28", "30", and "32". you don't need this IF statement)
if (typeof sizes[value] !== 'undefined') {
    for (var j=0; j<6; j++) {
        document.getElementById(sizes[value][j]).style.display = 'block';
    }
}

Store those variables in an object and you can use bracket/string notation to pull what you need: 将这些变量存储在对象中,然后可以使用方括号/字符串表示法提取所需的内容:

var allSizes = new Array(28,30,32);
var sizeGroups = {
    size28 : ["tgp_0", "tgp_1", "tgp_2", "tgp_3"],
    size30 : ["tgp_0", "tgp_1", "tgp_2"],
    size32 : ["tgp_0", "tgp_1", "tgp_2"]
}

var index = document.rightAngle.ddHP.selectedIndex;
var value = document.rightAngle.ddHP.options[index].value;

for (var i=0; i<allSizes.length; i++) {
    if (value == allSizes[i]) {
        var s = "size";
        for (var j=0; j<6; j++) {
            document.getElementById(sizeGroups[s+value][j]).style.display = 'block';
        }
    }
}

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

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