gpt4 book ai didi

javascript - For 循环运行数组的长度,但显示一个元素数组。length 次

转载 作者:行者123 更新时间:2023-12-01 03:59:22 25 4
gpt4 key购买 nike

我正在为 freeCodeCamp 开发一个项目,并且正在通过一组 twitch.tv 用户运行 for 循环。我创建了一个表,对于数组中的每个用户名,它应该添加新行,用数据填充它,然后移动到下一个元素。问题是,每次我运行代码时,它似乎都会在数组中选择一个随机索引并运行循环以等于数组中的元素数量。我认为这是一个显示问题,因为它为每个单独的调用连接到服务器。

希望有人能帮助我。

var twitch = ['ESL_SC2', 'OgamingSC2', 'cretetion', 'freecodecamp', 'storbeck', 'habathcx', 'RobotCaleb', 'noobs2ninjas', 'ESL_LOL', 'wow_2', 'brunofin', 'comster404']

var streams = 'https://wind-bow.gomix.me/twitch-api/streams/';
var channels = 'https://wind-bow.gomix.me/twitch-api/channels/';
var users = 'https://wind-bow.gomix.me/twitch-api/users/';


var getStream = function(data) {

if (data.stream === null) {
$('.status').append('Offline');
} else {
var streamStatus = data.stream.channel.status;
var html = '<td class="stream">' + streamStatus + '</td>';
$('.stream').html(html);
}

}
var getChannels = function(data) {
var game = data.display_name;

var logoHtml = data.logo;
var channelUrl = data.url;
var gameHtml = '<a href=' + channelUrl + ' target="_blank">' + game + '</a>';
var logoHtml = '<img class="image" src="' + logoHtml + '">';

$('.game').html(gameHtml);
$('.logo').html(logoHtml);

}


$(document).ready(function() {

$('.choice').on('click', function() {
$('.choice').removeClass('selected');
$(this).toggleClass('selected');
});

var table = $('<table id="twitch-table"></table>').appendTo('#content');

for (var i = 0; i < twitch.length; i++) {
var row = $('<tr></tr>').appendTo(table);
$('<td class="logo"></td>').appendTo(row);
$('<td class="game"></td>').appendTo(row);
$('<td class="stream"></td>').appendTo(row);

$.getJSON(streams + twitch[i], getStream, 'jsonp');
$.getJSON(channels + twitch[i], getChannels, 'jsonp');



}

});
body {
padding: 0;
margin: 0;
font-family: 'Oswald', sans-serif;
font-size: 100%;
}

a {
text-decoration: none;
}

#main {
width: 600px;
margin: 2% auto 0;
}

#header {
padding: 0;
margin: 0;
background: #116466;
color: #d1e8e2;
line-height: 100px;
width: 100%;
}

h1 {
margin: 0 0 0 5%;
font-size: 300%;
}

#row {
background: #285277;
width: 100%;
}

ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
justify-content: space-between;
}

.choice {
position: relative;
text-align: center;
width: 33%;
background: #285277;
padding: 5px 10px;
display: inline-block;
color: #d1e8e2;
font-size: 150%;
}

.choice a {
/*padding: 5px 20px;*/
color: #d1e8e2;
}

#content {
width: 100%;
background: #efefef;
}


/*
.choice a:active {
background: #1E3D59;
}
*/

.selected {
background: #1E3D59;
}

.selected:after {
content: '';
position: absolute;
top: 100%;
right: 45%;
width: 0;
height: 0;
border-top: solid 10px #1E3D59;
border-left: solid 10px transparent;
border-right: solid 10px transparent;
}

table {
width: 600px;
}

tr {
margin: 5px 0;
display: flex;
width: 100%;
justify-content: space-between;
}

td {
border-collapse: collapse;
box-sizing: border-box;
margin: 0;
padding: 0;
display: inline-block;
white-space: nowrap;
overflow: hidden;
}

.game {
padding-left: 10px;
width: 100px;
font-size: 120%;
line-height: 75px;
text-overflow: ellipsis;
}

.game a {
color: #111;
}

.image {
height: 75px;
width: 75px;
}

.logo {
padding: 3px 5px;
box-sizing: border-box;
}

.stream {
padding-right: 10px;
width: 350px;
display: inline-block;
text-overflow: ellipsis;
line-height: 75px;
}
<!DOCTYPE html>

<html>

<head>
<title>Twitch.tv JSON API</title>
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<script src="https://use.fontawesome.com/57c9bf8971.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>

<body>
<div id='main'>
<header id='header'>
<h1>Twitch Streamers</h1>
</header>
<div id='row'>
<ul>
<li class='choice selected'><a href='#'>All</a></li>
<li class='choice'><a href='#'>Online</a></li>
<li class='choice'><a href='#'>Offline</a></li>
</ul>
</div>
<div id='content'>

</div>
</div>
</body>

</html>

最佳答案

问题出在以下几行:

$('.game').html(gameHtml);
$('.logo').html(logoHtml);
$('.stream').html(html);

他们选择所有具有 game 类(或 logostream)的行并将其全部更改。

参见here一个固定的 JSfiddle。

关于javascript - For 循环运行数组的长度,但显示一个元素数组。length 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42324646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com