我在使用 jQuery 的 .append()
方法时遇到问题。这是我的代码:
$('#all ul li').each(function() {
if ($(this).children('.material-icons').hasClass('offline-icon') == false) {
$('#online ul').append(this);
}
});
$(document).ready(function() {
// Declare all variables
var users = ["freecodecamp", "trymacs", "phonecats"];
var info = {};
var i;
var identifier;
for (i = 0; i < users.length; i++) {
$.getJSON("https://wind-bow.gomix.me/twitch-api/streams/" + users[i], function(json) {
//create key for each object
identifier = json["_links"].self;
identifier = JSON.stringify(identifier);
identifier = identifier.substr(identifier.lastIndexOf('/') + 1);
identifier = identifier.slice(0, -1);
// Add key/value pair to object
info[identifier] = JSON.stringify(json);
// Check if user is streaming using length of object
if (info.freecodecamp.length < 400) {
$('#freecodecamp .material-icons')
.addClass('offline-icon')
.html('portable_wifi_off');
}
if (info.trymacs.length < 400) {
$('#trymacs .material-icons')
.addClass('offline-icon')
.html('portable_wifi_off');
}
if (info.phonecats.length < 400) {
$('#phonecats .material-icons')
.addClass('offline-icon')
.html('portable_wifi_off');
}
});
}
// Filter content into online section
$('#all ul li').each(function() {
if ($(this).children('.material-icons').hasClass('offline-icon') == false) {
$('#online ul').append(this);
}
});
/*$.getJSON("", function(json) {
// Only change code below this line.
json = json.filter(function(val) {
return (val.id !== 1);
});
// Only change code above this line.
json.forEach(function(val) {
});
});
// Thanks to Tyler Moeller for his help with this function
/*users.map(function(val) {
var url = 'https://wind-bow.gomix.me/twitch-api/streams/' + val
$.getJSON(url, logTheData);
});
function logTheData(data) {
status.push(data);
}
console.log(status)
});*/
});
body {
background-color: pink;
}
#app {
margin-top: 50px;
padding: 0;
}
.head {
background-color: #ee6e73;
padding: 15px
}
.head h1 {
margin: 0;
font-size: 43px;
color: #fff;
text-shadow: 0.012em 0.012em #ccc;
}
.collection, .collection-item {
border: none;
border-bottom: none !important;
}
.collection-item img {
height: 40px
}
.filter {
background-color: #fff;
padding: 0 !important;
}
.filter ul {
margin: 0;
}
.filter ul li {
border-top: 1px solid #eee;
}
img {
border: 2px solid #ddd;
}
.offline-icon {
color: #e53935;
}
<div class="row">
<div class="col m8 offset-m2 s12 z-depth-1" id="app">
<div class="head">
<h1>Twitch.tv Streamers</h1>
</div>
<ul class="tabs">
<li class="tab col s3"><a class="active" href="#all">All</a></li>
<li class="tab col s3"><a href="#online">Online</a></li>
<li class="tab col s3"><a href="#offline">Offline</a></li>
</ul>
<div id="all" class="col s12 filter">
<ul class="collection">
<li class="collection-item" id="freecodecamp">
<img src="https://static-cdn.jtvnw.net/jtv_user_pictures/freecodecamp-profile_image-d9514f2df0962329-300x300.png" alt="" class="circle">
FreeCodeCamp
<i class="secondary-content material-icons">wifi_tethering</i>
</li>
<li class="collection-item" id="trymacs">
<img src="https://static-cdn.jtvnw.net/jtv_user_pictures/trymacs-profile_image-3c2ac4643bab750a-300x300.jpeg" alt="" class="circle">
Trymacs
<i class="secondary-content material-icons">wifi_tethering</i>
</li>
<li class="collection-item" id="phonecats">
<img src="https://static-cdn.jtvnw.net/jtv_user_pictures/phonecats-profile_image-a536a337b2e58260-300x300.png" alt="" class="circle">
phonecats
<i class="secondary-content material-icons">wifi_tethering</i>
</li>
<!--<li>Item <i class="material-icons">portable_wifi_off</i></li>
<li>Item <i class="material-icons">wifi_tethering</i></li>
<li>Item</li>-->
</ul>
</div>
<div id="online" class="col s12 filter">
<ul class="collection">
</ul>
</div>
<div id="offline" class="col s12 filter">
<ul>
<li>Item</li>
<li>Item</li>
<li>Item</li>
</ul>
</div>
</div>
</div>
本质上,我只希望没有 offline-icon
类的列表项出现在在线选项卡中。但是,我的代码将所有项目从“所有”选项卡移动到“在线”选项卡。
提前感谢您的帮助。
最佳答案
我发现问题:
http://codepen.io/anon/pen/qREqZg?editors=0110
将代码放在 get json 函数中 - 例如,在添加类下。
当您检查类(文档准备好/ajax 调用未完成)时,它们不存在,因此现在应该修复它。
if (info.phonecats.length < 400) {
$('#phonecats .material-icons')
.addClass('offline-icon')
.html('portable_wifi_off');
}
$('#all ul li').each(function() {
if ($(this).children('.material-icons').hasClass('offline-icon') == false) {
$('#online ul').append($(this).clone());
}
});
编辑: 所以,基本上,由于 DOM 已更新,在 ajax 请求之后,等待成功,然后将您的代码片段添加到,例如done() 或always() 回调:http://api.jquery.com/jquery.getjson/
关于javascript - 使用 jQuery 追加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41476119/