简体   繁体   English

如何通过HTML循环获取元素?

[英]how do I get elements by loop, html?

I want to do something like: 我想做类似的事情:

var i=0;

while(i<20)
{
  var temp= document.getElementById(''+20);
  $(temp).hide();
  i++;
}

Can I do that? 我可以那样做吗? I tried, but it didn't work. 我尝试过,但是没有用。

Yes, example: 是的,例如:

for (var i=0; i < 20; i++) {
  var temp = document.getElementById(i);
  console.log(temp);
}

A working sample: http://jsfiddle.net/bqF9T/ 工作示例: http : //jsfiddle.net/bqF9T/

But as noted in the comments, be sure to use valid IDs as what happens to work in Chrome may not in other browsers if the HTML is invalid. 但是请注意,如注释中所述,请确保使用有效的ID,因为如果HTML无效,那么在Chrome浏览器中可能会发生的事情可能无法在其他浏览器中使用。

Try this: 尝试这个:

var i = 0;

while(i < 20){
    var temp = document.getElementById('Elid_'+i);
    // DO STUFF
    i++;
}

You had an invinite loop (i was always smaller then 20) Also did you use the static number 20 instead of i. 您有一个invinite循环(我总是小于20)还使用了静态数字20代替了i。 the more or less dynamic number And as mentiod else where, an ID can't start with only an number... 或多或少的动态数字以及在其他情况下,ID不能仅以数字开头...

You have actually created an infinite loop. 您实际上已经创建了一个无限循环。 Since you don't change the value of i, it will be 0 forever, and thus always < 20. 由于您不更改i的值,因此它将永远为0,因此始终<20。

You should also give your elements ids that aren't just numbers (as that won't work). 您还应该给您的元素ID不仅是数字(因为这是行不通的)。

<div id='something1'></div>

Try a for loop, like this: 尝试一个for循环,像这样:

// Wait for the page to finish loading.
window.onload = function() {
    // Run your loop.
    for(var i = 0; i < 20; i++) {
        var temp = document.getElementById('something' + i);

        // Do stuff with temp.
    }
}

// Or, with jQuery
$(document).ready(function() {
    // Run your loop.
    for(var i = 0; i < 20; i++) {
        var $temp = $('#something' + i);

        $temp.hide();

        // Or, if you're only hiding...
        $('#something' + 1).hide();
    }
});

ID's can't begin with a number , so you have to have something before the count. ID不能以数字开头 ,因此您必须在计数前先输入一些内容。

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). ID和NAME令牌必须以字母([A-Za-z])开头,后跟任意数量的字母,数字([0-9]),连字符(“-”),下划线(“ _”) ,冒号(“:”)和句点(“。”)。

So prefix it with something else to make it valid, I chose " example_ ". 因此,在它example_加上其他名称以使其生效,我选择了“ example_ ”。

The other problem is that you're always trying to get the element with "20" in the ID. 另一个问题是,您始终尝试获取ID中带有“ 20”的元素。 You want to get all of them so you have to use your loop counter, i . 您想获得所有这些,因此必须使用循环计数器i

Also note that the counter goes from 0 to 19 (when i = 20 , i<20 is FALSE) so keep that in mind so it matches with your markup. 另请注意,计数器从0变为19(当i = 20i<20为FALSE),因此请记住这一点,使其与您的标记匹配。

JS: JS:

var i=0;

while(i<20)
{
  var temp= document.getElementById('element_' + i);
  $(temp).hide();
  i++;
}

with your markup looking like: 您的标记看起来像:

<div id="element_0">first</div>
<div id="element_1">second</div>
<div id="element_2">foo</div>
...
<div id="element_19">bar</div>

if you have a collection of elements where id="htmlID1-20" you can change the code to somthing like this: 如果您有id =“ htmlID1-20”的元素集合,则可以将代码更改为以下形式:

var i = 20
  , elems = [] // empty array to keep the elements
while(i--){  //this trick reduces the variable i by 1 each time the loop runs, stopping at 0
    elems.push(document.getElementById("htmlID"+i)) //collect the dom elements and push a refrance to it in the array
}
console.log(elems)

it is not recomendet to create variabels or functions inside loops 建议不要在循环内创建变量或函数

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

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