简体   繁体   English

使用javascript动态删除项目并将其添加回屏幕

[英]dynamically remove and add back items to the screen with javascript

I am stuck, I am working with tables so using Div does not seem to work to let me get rid of stuff, but this does work, I just cannot figure out how to use it dynamically. 我被困住了,我正在使用表,因此使用Div似乎无法摆脱任何东西,但这确实可行,我只是无法弄清楚如何动态使用它。

document.all.???.style.display="none" document.all.???.style.display="none“

My idea is to collect the item names in an array and then use the array to remove the desired stuff from the screen. 我的想法是将项目名称收集到一个数组中,然后使用该数组从屏幕上删除所需的内容。 I just cannot figure out how using javascript and vbscript to collect the array, and then to retrieve the value in the javascript function. 我只是无法弄清楚如何使用javascript和vbscript来收集数组,然后在javascript函数中检索值。 That same approach works inline with the code, to supress items that I don't want to show such as headings with no detail. 相同的方法可以与代码内联,以抑制我不想显示的项目,例如标题不详细的项目。

In that case I use a vbscript variable and document.all.<%=x%>.style.display="none"; 在那种情况下,我使用vbscript变量和document.all。<%= x%>。style.display =“ none”;

How can I get X out of the array or directly reference the array in this statement? 我怎样才能从数组中取出X或在此语句中直接引用该数组?

You can get an array of the elements with a particular name using document.getElementsByName(nameOfElement); 您可以使用document.getElementsByName(nameOfElement);获得具有特定名称的元素数组document.getElementsByName(nameOfElement); So something like this should work for you: 所以这样的事情应该为您工作:

var arrayOfElementNames = ['Name1', 'Name2']
for (var nameIndex = 0; nameIndex < arrayOfElementNames.length; nameIndex++) {
  var elements = document.getElementsByName(arrayOfElementNames[nameIndex]);
  for (var elementIndex = 0; elementIndex < elements.length; elementIndex++) {
    elements[elementIndex].style.display = 'none';
  }
}

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

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