我正在尝试创建一个调用api并获取用户数据的简单应用。当用户数据显示在页面上时,我想用用户名搜索用户。尝试了很多次,但它不起作用。有人可以帮忙吗?
var base = "https://api.twitch.tv/kraken/channels/";
var key = "myKey&callback";
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp",
"storbeck", "habathcx", "RobotCaleb", "noobs2ninjas",
"TwinGalaxiesLive","MedryBW", "frontender007","Gronkh"];
$(document).ready(function(){
requestData(users);
searchUsers($("a"));
});
function requestData ( arr ) {
arr.forEach ( function ( user ) {
var url = base + user + '/?client_id=' + key + '&callback=?';
$.getJSON(url, function ( json ) {
displayChannel(json);
});
});
}
function displayChannel( obj ) {
var name, status, logo, url, game;
name = obj.display_name;
status = obj.status;
logo = obj.logo;
url = obj.url;
game = obj.game;
var channel,onoff;
if ( status && status.length > 16) {
status = status.substr(0, 16) + "....";
}
if ( game === null ) {
onoff = "offline";
}
else {
onoff = "online";
}
channel = "<a>";
channel += "<h3>" + name + "</h3>";
channel += "<img src='" + logo + "'>";
channel += "<p>" + status + "</p>";
$(channel).attr('class', onoff);
$("#container").append(channel);
}
function searchUsers( arr ) {
var searchBox = $("input[type='text']");
$(searchBox).keyup( function(){
var chars = $(searchBox).val();
$("arr").each(function (item){
if($(item).find('h3').text().indexOf(chars) < 0) {
$(this).parent().closest('a').hide();
}
else {
$(this).parent().closest('a').show();
}
});
});
}
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/************************************************************/
header {
width: 960px;
height: auto;
margin: 20px auto;
overflow: hidden;
}
header form {
float: right;
}
header form input {
font-size: 18px;
padding: 5px 10px;
}
#container {
width: 960px;
height: 500px;
background-color: #cecece;
margin: 50px auto;
}
a {
width: 140px;
height: 80px;
margin: 40px;
float: left;
}
a img {
width: 140px;
height: 80px;
}
h3 {
margin-bottom: 10px;
float: left;
}
p {
font-style: italic;
font-size: 13px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="twitch.css">
<title> Twitch TV</title>
</head>
<body>
<header>
<form action="#">
<input type="text">
<input type="radio"> All
<input type="radio"> Online
<input type="radio"> Offline
</form>
</header>
<div id="container">
</div>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="twitch.js"></script>
</body>
</html>
答案 0 :(得分:1)
我尝试更新您的代码,希望这有帮助
var base = "https://api.twitch.tv/kraken/users/";
var key = "myKey&callback";
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp",
"storbeck", "habathcx", "RobotCaleb", "noobs2ninjas",
"TwinGalaxiesLive","MedryBW", "frontender007","Gronkh"];
$(document).ready(function(){
requestData(users);
searchUsers(users);
});
function requestData ( arr ) {
arr.forEach ( function ( user ) {
var url = base + user + '/?client_id=' + key + '&callback=?';
$.getJSON(url, function ( json ) {
displayChannel(json);
});
});
}
function displayChannel( obj ) {
var name, status, logo, url, game;
name = obj.display_name;
status = obj.status;
logo = obj.logo;
url = obj.url;
game = obj.game;
var channel,onoff;
if ( status && status.length > 16) {
status = status.substr(0, 16) + "....";
}
if ( game === null ) {
onoff = "offline";
}
else {
onoff = "online";
}
channel = "<a id="+name+">";
channel += "<h3>" + name + "</h3>";
channel += "<img src='" + logo + "'>";
channel += "<p>" + status + "</p>";
$(channel).attr('class', onoff);
$("#container").append(channel);
}
function searchUsers( arr ) {
var searchBox = $("input[type='text']");
var chars = $(searchBox).val();
$.each( arr, function( i, val ) {
if(val==chars) {
$('a#'+chars).siblings().hide();
}
else {
$(this).parent().closest('a').show();
}
});
}
$("input[type='text']").change(function(){
searchUsers(users);
});