简体   繁体   English

我在使javascript列表不确定长度时遇到麻烦,仅在分配某些属性时才显示

[英]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"

JSBin here JSBin 在这里

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

相关问题 我在理解此Javascript函数时遇到问题 - I'm having trouble understanding this Javascript function 我在用JavaScript读取Cookie时遇到问题 - I'm having trouble reading a cookie in javascript 我在制作倒数计时器时遇到问题(Reactjs) - I'm having trouble in making a countdown timer (Reactjs ) 我在制作播放音乐的 discord 机器人时遇到问题 - I'm having trouble making a discord bot that plays music 我在输出JavaScript时遇到问题 - I'm having trouble having my JavaScript output 在javascript中,我无法弄清楚如何使正则表达式只替换捕获而不是匹配 - In javascript, I'm having trouble figuring out how to make a regex replace only the capture and not the match 使用Javascript从列表中提取项目属性时遇到问题 - Having trouble using Javascript to pull item attributes from a list 我无法使用 Javascript 和 HTML DOM 元素在我的产品选项旁边正确显示 svg 工具提示 - I'm having trouble making an svg tooltip appear correctly next to my product options using Javascript and HTML DOM elements 尝试在图像之间切换时,Javascript Onclick事件有点麻烦 - I'm having a bit of trouble with the Javascript Onclick event when trying to toggle between images 我在制作JavaScript打字机效果时遇到问题 - I am having trouble making a javascript typewriter effect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM