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