style.display 为 null - 尝试循环数组并更改显示属性
我的另一个新手问题。
只是检查我下面的代码是否可行。即创建一个由“document.GetElementById's”组成的新数组。由于我对 javascript 相当陌生,所以我的代码通常有点冗长,所以请原谅我的混乱。
下面的代码引发错误“style.display”为“null”或不是对象。 任何人都可以看到我遗漏或做错的明显事情吗?
function Test(){
if(document.getElementById('inClient').value !=="FormViewer"){
var visible =new Array("document.getElementById('personal2').value","document.getElementById('change_hours2').value");
var change = new Array("document.getElementById('personal').value","document.getElementById('change_hours').value");
for (var i=0; i <visible.length; i++) {
if(visible[i]!==""){
change[i].style.display = "block"
}
}
}
}
基本上,如果隐藏字段(“personal2”等)为空,我希望 div/部分(“个人”等)保持隐藏,但如果它包含文本,那么我想显示该部分。
提前致谢
Another newbie question from me.
Just checking whether my code below is possible. I.e creating a new array consisting of 'document.GetElementById's'. As i'm fairly new to javascript my code is generally a bit long winded, so please forgive the messiness.
The code below is bringing up the error 'style.display' is 'null' or not an object.
Can anyone see anything obvious i'm missing or doing wrong?
function Test(){
if(document.getElementById('inClient').value !=="FormViewer"){
var visible =new Array("document.getElementById('personal2').value","document.getElementById('change_hours2').value");
var change = new Array("document.getElementById('personal').value","document.getElementById('change_hours').value");
for (var i=0; i <visible.length; i++) {
if(visible[i]!==""){
change[i].style.display = "block"
}
}
}
}
Basically if the hidden fields ('personal2' etc) are blank i want the div / section ('personal' etc) to remain hidden but if it contains text then i want to show the section.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在将字符串添加到数组中,而不是对 DOM 元素的引用。删除引号和
value
属性,它将起作用。您必须检查迭代中的值
。You are adding strings to your array instead of references to DOM elements. Remove the quotes and the
value
property and it will work. You have to check thevalue
within the iteration.你应该像这样使用,没有双引号,它应该可以工作。
You should use like this, no double qoutes, it should work.