简体   繁体   English

有条件的情况下可变使用不起作用?

[英]Variable use in conditional not working?

So I have 10 of these in my HTML: 所以我的HTML中有10个:

<div class="resultContainer">
   <div class="twitchResult" id="charionna">
      <a class="link" href="https://www.twitch.tv/charionna"target="_blank">
         <h3 class="username">charionna</h3>
         <p class="streamInfo"></p>
      </a>
   </div>
</div>

When I make an request to the Twitch API: 当我向Twitch API发出请求时:

 var xhr = new XMLHttpRequest();
 xhr.open('GET', 'https://api.twitch.tv/kraken/streams/comster404/?client_id=mlrx1e94dg7yus5yqm26lwpyxrg9j9x');
 xhr.onreadystatechange = twitchInfo;
 xhr.send();

and the server responds with a non-200 status code I have the response handler function run the callSuccessChecker function like this: 并且服务器以非200状态代码响应我让响应处理程序函数像这样运行callSuccessChecker函数:

function twitchInfo() {


     // request NOT succesful
     if(this.readyState == 4 && this.status !== 200) {

     //  console.log('This is working');

     callSuccessChecker();

     }
}

This part is working just fine. 这部分工作正常。

It's the callSuccessChecker that's an issue. 这是callSuccessChecker的问题。 Specifically, using a variable that's I declared in it in a conditional. 具体来说,使用在条件中声明的变量。 Like so: 像这样:

function callSuccessChecker() {

      var containerList = document.getElementsByClassName('resultContainer');
      var closedText = document.createTextNode('account closed');

      for (i = 0; i < 10; i++) {// <--------not working YET

          if(containerList[i].style.backgroundColor == 'rgb(245, 209, 107)') {

          containerList[i].children[0].children[0].children[1].appendChild(closedText);
          }
     // console.log (containerList[i].children[0].children[0].children[1]);
     }
}

In this function you can see I have commented out a console.log at the bottom. 在此功能中,您可以看到我在底部注释掉了console.log。 When I use containerList[i] here, it shows me the output in the console. 当我在此处使用containerList[i] ,它将向我显示控制台中的输出。

But when I do the same thing (as is visible in the function) inside the if statement and object, it doesn't seem to work/activate/grab it/whatever the correct term for that is. 但是,当我在if语句和对象内执行相同的操作(在函数中可见)时,无论正确的用语是什么,它似乎都无法工作/激活/抓取。

Here are the styles for the container whose backgroundColor I'm trying to check against in the conditional (I converted the hex code into its RGB equivalent in the conditional): 这是我要在条件中检查其backgroundColor的容器的样式(我在条件中将十六进制代码转换为其等效的RGB):

.resultContainer {
    height: auto;
    margin-right: 10%;
    margin-left: 10%;
    margin-bottom: 0;
    border-bottom: 1px white solid;
    background-color: #F5D16B;
}

Any explanations as to why this is? 关于这是为什么的任何解释?

Is it the conditional itself that is off? 是条件性条件本身关闭了吗?

Help is greatly appreciated. 非常感谢您的帮助。

There might be problem of comparsion of color value. 可能存在色值比较的问题。 How about: 怎么样:

function isEqualColor(a, b) {
    var elementA = document.createElement("div"),
        elementB = document.createElement("div");
    elementA.style.backgroundColor = a;
    elementB.style.backgroundColor = b;
    return elementA.style.backgroundColor == elementB.style.backgroundColor;
}

function callSuccessChecker() {
    var containerList = document.getElementsByClassName('resultContainer');
    var closedText = document.createTextNode('account closed');

    for (i = 0; i < 10; i++) {
        if(isEqualColor(containerList[i].style.backgroundColor, '#F5D16B')) {
            containerList[i].children[0].children[0].children[1].appendChild(closedText);
        }
        console.log(containerList[i].children[0].children[0].children[1]);
    }
}

This block of console.logs should help you determine where in the chain the object is breaking: 这个console.logs块应该可以帮助您确定对象在链中的哪个位置断开:

if(containerList[i].style.backgroundColor == 'rgb(245, 209, 107)') {
    console.log(containerList[i]);
    console.log(containerList[i].children[0]);
    console.log(containerList[i].children[0].children[0]);
    console.log(containerList[i].children[0].children[0].children[1]);
    containerList[i].children[0].children[0].children[1].appendChild(closedText);
}

If all of these print out non-null, then the appendChild() should work. 如果所有这些打印出来的结果都不为空,则appendChild()应该可以工作。

You can also try a different way of reading backgroundColor , like so: 您还可以尝试以其他方式读取backgroundColor ,如下所示:

if(containerList[i].currentStyle['background-color'] == 'rgb(245, 209, 107)') {
    //...
}

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

相关问题 AngularJS-条件属性不能与变量一起使用? - AngularJS - Conditional attribute not working with variable? 将查询字符串数据存储到变量中以便在条件中使用 - Store Query String Data Into Variable For Use In Conditional 如何在变量中获取这个值并在 React 的条件中使用它? - How to get this value in a variable and use it in a conditional in React? 为何使用!! 将变量强制转换为布尔值以用于条件表达式? - Why use !! to coerce a variable to boolean for use in a conditional expression? 在JavaScript中,可以在条件语句中声明变量后使用toLowerCase()吗? - In JavaScript, can you use toLowerCase() after a variable is declared in a conditional statement? JavaScript变量声明/初始化可以使用条件类型的布尔值吗? - Can a JavaScript variable declaration/initialization use a boolean type conditional? 如何使用状态变量有条件地声明对象数组? - How to use state variable for conditional declaring of array of objects? 在 React 中使用条件渲染但未定义存储元素的变量? - Use conditional rendering in React but a variable storing the element is not defined? Javascript - 可以在 HTML 变量声明中使用简写条件吗? - Javascript - Possible to use a shorthand conditional within an HTML variable declaration? 可变条件未按预期声明 - Variable conditional not declaring as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM