[英]I'm having trouble making a javascript list on indeterminate length only display when certain attributes are assigned
I have a chatbox and I'm making a block function. 我有一个聊天框,正在执行阻止功能。 I've pretty much got the code but it isn't working (Java isn't my language).
我几乎已经有了代码,但是它不起作用(Java不是我的语言)。
I've got the attribute and I can make it disappear but I cannot do it recursively. 我有该属性,可以使其消失,但不能递归执行。 In many cases the code stops working altogether.
在许多情况下,代码会完全停止工作。
<table>
<td userclass="" username="cat">cat</td>
<td userclass="" username="hampster">hamster</td>
<td userclass="" username="dog">dog</td>
</table>
<p id="message"></p>
<script>
var x = document.getElementsByTagName("td");
var i;
for (i = 0; i < x.length; i++) {
var btn = document.getElementsByTagName("td")[i];
var x = btn.attributes[1].value;
if (x="hamster")
{
document.getElementsByTagName("td")[i].style.visibility = "hidden";
}else{
document.getElementsByTagName("td")[i].style.visibility = "visible";}
}
</script>
Success occurs before I include the 'For' loop Error usually doesn't do anything at all 成功发生在我包含“ For”循环之前,错误通常根本不起作用
var
defines a variable in the global scope. var
在全局范围内定义变量。 You are using it to store the list of <td>
and the value of an attribute. 您正在使用它来存储
<td>
列表和属性值。 Try using different variable names first: 首先尝试使用不同的变量名:
var myTds = document.getElementsByTagName("td");
var i;
for (i = 0; i < myTds.length; i++) {
var btn = myTds[i];
var x = btn.attributes[1].value;
if (x=="hamster") {
myTds[i].style.visibility = "hidden";
} else {
myTds[i].style.visibility = "visible";
}
}
Also note that: 另请注意:
1) You should use ==
for comparisons; 1)您应该使用
==
进行比较;
2) you do not need to use document.getElementsByTagName("td")
. 2)您不需要使用
document.getElementsByTagName("td")
。
3) Change username="hampster"
to username="hamster"
in your HTML to see the example working. 3)在您的HTML
username="hampster"
更改为username="hamster"
,以查看示例的工作username="hampster"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.