简体   繁体   English

使用带有CSS的jQuery ID Selector

[英]Use jQuery ID Selector with CSS

I'm actually creating a list with streamers and checking if they're online, if so they'll get a green background color. 我实际上正在创建一个带有飘带的列表并检查它们是否在线,如果是这样,它们将获得绿色背景颜色。

Now I want to create an additional list next to my "team list" which includes every streamer. 现在我想在我的“团队列表”旁边创建一个额外的列表,其中包括每个流媒体。


For this I'm trying to use the same method like the one I'm using for the background color. 为此,我试图使用与我用于背景颜色的方法相同的方法。

$(".".concat(user)).css("background-color","lightgrey");

(to color every background color for the streamers) (为着色器的每种背景颜色着色)

But now I want to just hide the streamers in one list and not everywhere. 但现在我想把一个列表中的飘带隐藏起来,而不是到处都是。

So I can't use this: 所以我不能用这个:

$(".".concat(user)).css("display", "none");

(would hide every offline streamer in every list ) (会隐藏每个列表中的 每个离线流媒体)


I already tried to apply the ID but actually I don't know how. 我已经尝试过应用ID但实际上我不知道如何。 If I use this: 如果我用这个:

$("#online".concat(user)).css("display", "none");

nothing happens. 什么都没发生。

(I named my ID online) (我在网上命名我的身份证)

<tr class="header" id ="online">

(I already tried to apply the id to a new , to the and now to the but nothing works) (我已经尝试将id应用到一个新的,现在到了但没有任何作用)


TL;DR: TL; DR:

I want to hide offline streamers in a new list but I can't use the same ID selector as I used before. 我想在新列表中隐藏脱机流媒体,但我不能使用与之前使用的相同的ID选择器。 How can I do it? 我该怎么做?

Not much to start with, but if you have a structure like 开头并不多,但如果你有一个类似的结构

    <table>
        <tr class="header Peter" id ="onlinePeter">
            <td>Peter</td>
        </tr>
        <tr class="header John" id ="onlineJohn">
            <td>John</td>
        </tr>
        <tr class="header Michael" id ="onlineMichael">
            <td>Michael</td>
        </tr>
        <tr class="header Bob" id ="onlineBob">
            <td>Bob</td>
        </tr>
        <tr class="header Daniel" id ="onlineDaniel">
            <td>Daniel</td>
        </tr>
    </table>

and a script like 和像这样的脚本

var user = "John";
$("#online".concat(user)).css("display", "none");
user = "Daniel"
$(".".concat(user)).css("background","lightgrey");

It works. 有用。 the row that contains John disappears 包含John的行消失了

On the other piece of code where you try to change the background color, you are calling an element with the class = user, but the HTML you provided only has the class "header", once the class is added, the background changes as well. 在尝试更改背景颜色的另一段代码中,您使用class = user调用元素,但是您提供的HTML只有类“header”,一旦添加了类,背景也会更改。

I looked at the jsfiddle you linked and got a better understanding of what is happening. 我查看了您链接的jsfiddle,并更好地了解了正在发生的事情。

The server returns an empty stream all the time, so the code never goes into the "true" part of the statement. 服务器一直返回一个空流,因此代码永远不会进入语句的“true”部分。 Furthermore, in the "false" part of the statement, the code changes the background color and then hides the table cell. 此外,在语句的“false”部分,代码更改背景颜色,然后隐藏表格单元格。

 function colorize(user){
     $.getJSON('https://api.twitch.tv/kraken/streams/'+user, function(channel) {
         /*
         * Stream always returns as null
         */

         console.log(channel["stream"]); // returns null in all tests
         var data = channel["stream"]; // data == null
         var boolAnswer = ((typeof(data) !== 'undefined') && (data !== null));
         console.log(data);
         if ( boolAnswer === true) {
             $( ".".concat(user) ).css("background-color", "lightgreen");
             /*
             * boolAnswer is always null, so this piece is skipped all the time
             */
         }
         else if ( boolAnswer === false){
             /*
             * First it changes the color and then it hides it
             */
             $(".".concat(user)).css("background-color","lightgrey");
             $(".".concat(user)).css("display", "none");

         }
     }).success(function(response){
         console.log(response);
     });
 }

colorize('jankos'); //hides the cell of jankos
colorize(); // doesn't do anything

if you comment the line that says $(".".concat(user)).css("display", "none"); 如果你评论说$(“。”。concat(user))。css(“display”,“none”);

colorize('jankos'); //changes the color of the cell of jankos  
colorize() // still doesn't do anything

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM