【问题标题】:Why my jquery hide and show methods are not working for online offline link为什么我的 jquery 隐藏和显示方法不适用于在线离线链接
【发布时间】:2017-07-05 05:56:05
【问题描述】:

我有一个显示 twitchTV 频道和用户的小应用程序。当我点击“在线”链接时,只显示在线用户。当我点击时也是如此 “离线”链接。 (navigation var) 问题是我没有看到在页面上生效的逻辑。我错过了什么?谢谢

var status;
var names = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas"]
var url = "https://wind-bow.gomix.me/twitch-api/";

$(document).ready(function() {
  $("#allusers").click(function() {
    $("#onlineuser").show();
    $("#offlineuser").show();
  });
  $("#online").click(function() {
    $("#onlineuser").show();
    $("#offlineuser").hide();

  });
  $("#offline").click(function() {
    $("#offlineuser").show();
    $("#onlineuser").hide();
  });
  getData();
});

function getData() {
  //for every single stream/channel a url need build
  for (var i = 0; i < names.length; i++) {
    getStreams(url, "streams", names[i]);
    getChannel(url, "channels", names[i]);
  }
}

//getting stream status json data
function getStreams(url, type, name) {
  url += '/' + type + '/' + name + '?callback=?';
  // console.log(url);
  $.getJSON(url, function(data) {
    console.log(data);
    if (data.stream !== null) {
      status = "online";
      // console.log(data);
    } else {
      status = "offline";
    }
  });
}

//getting channel json data
function getChannel(url, type, name) {
  url += type + '/' + name + '?callback=?';
  // console.log(url);
  $.getJSON(url, function(data) {
    // console.log(data);
    var output = document.getElementById("output");
    var logo = data.logo;
    var title = data.status;
    var channel = data.url;
    // console.log(logo +", "+title+", "+channel);
    //formatting individual channels

    output.innerHTML += '<div id="' + status + 'user" class="channel row user-data"><div ><img id="logo" src="' + logo + '"></div><a href="' + channel + '" target="_blank"><h5 id="user">' + name + '</h5></a><div class="col-md-2 user-data"><strong>' + status + '</strong></div><div id="title" class="col-md-5 user-data">' + ((title.length > 40) ? title.substring(0, 40).concat("...") : title) + '</div></div>';
  });
}
body {
  background-color: #445f44;
}

.user-data {
  padding-top: 10px;
  padding-left: 5px;
  padding-right: 5px;
  color: black;
  font-size: 17px;
  background-color: white;
  opacity: 0.5;
  margin-bottom: 5px;
}

#header {
  background-color: #333854
}

#user {
  font-size: 20px;
  padding-left: 10px;
  padding-right: 10px;
}

img {
  width: 50px;
  height: 50px;
  border-radius: 300px;
  border-color: green;
  border-width: 1px;
  border-style: solid;
}

.border {
  border-style: solid;
  border-width: 1px;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container" class="container ">
  <div id="header" class="jumbotron" id="header">
    <h1 class="text-center" style="color: black;">TwitchTV Streamers</h1>
    <ol class="breadcrumb">
      <li class="active"><a id="allusers" href="#"><strong>All Users</strong></a></li>
      <li><a id="online" href="#"><strong>Online</strong></a></li>
      <li><a id="offline" href="#"><strong>Offline</strong></a></li>
    </ol>
  </div>
  <!-- output here!   -->
  <div class="row">
    <div id="output" class="col-lg-12">
    </div>
  </div>
</div>

【问题讨论】:

  • jquery 是页面上加载的第一个文件吗?您的错误消息“Uncaught ReferenceError: $ is not defined”意味着它找不到您的 jquery 文件
  • 编辑了 stacksn-p 以包含 jQuery,现在似乎可以加载了。 codepen 中的版本实际上似乎按您说的那样工作?
  • 您可能需要使用承诺。您正在调用 getStreams 和 getChannels 并且这些函数中的每一个都在执行 GET 请求。 getStreams 不会等到返回响应来继续执行您的代码。它立即开始执行 getChannels。

标签: javascript jquery html


【解决方案1】:

似乎您正在尝试使用 id 来定位多个元素,请尝试使用类。

例如:

$("#online").click(function(){
  $(".onlineuser").show();
  $(".offlineuser").hide();
});

output.innerHTML += '<div class="'+status+'user channel row user-data"><div ><img id="logo" src="'+logo+'"></div><a href="'+channel+'" target="_blank"><h5 id="user">'+name+'</h5></a><div class="col-md-2 user-data"><strong>'+status+'</strong></div><div id="title" class="col-md-5 user-data">'+((title.length > 40) ? title.substring(0, 40).concat("...") : title)+'</div></div>'; 

链接到具有这些更改的代码笔:https://codepen.io/anon/pen/awKBEM

【讨论】:

  • 是的,你完全正确?...我认为这就是问题所在...等等!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多