Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    aggiungere classe a <li>

    Fra i tanti tentativi ho provato questo ma non va:

    codice:
    jQuery( '.onlineusers ul li#u-40').removeClass('on off').addClass('on');
    quello che vorrei ottenere è togliere le classi on e off, se presenti, ed aggiungere la classe on. Il tutto in document ready...

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    perche non rimuovi solo la classe off?

  3. #3
    il mio problema è che non mi va ad aggiungere la classe. ovviamente il tutto viene gestito con delle variabili quindi se è presente la classe off la deve sostituire con on e viceversa.

    La domanda è: come faccio ad aggiungere una classe ad un <li> con id noto?

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    http://api.jquery.com/addclass/


    $("#id").addClass("classe");

    nel tuo caso:

    $("#u-40").addClass("on");


  5. #5
    non mi sembra vero di rimanere bloccato 2 giorni per una cosa tanto stupida... e non venirne ancora a capo.
    Credo che il problema sia legato al fatto che l'elenco <ul> venga caricato con ajax.
    In effetti ho una pagina chat.php che mi carica gli utenti in questo elenco con un ciclo php. Questa pagina (che ha come unico output l'elenco degli utenti) viene chiamata con ajax. La classe che devo assolutamente aggiungere mi serve per vedere gli utenti online.

    Invio l'intera pagina nella speranza che qualcuno voglia aiutarmi ad uscire da questa zona d'ombra:
    codice:
    <?php 
    require_once('../php/functions.php');
    sec_session_start();
    
    $id_user     = $_SESSION['user_id'];
    ?>
    
    <div id="chatwindows" class="chatwindows"></div><!--chatwindows-->
    
    <div class="onlineuserpanel">
        <div id="chatscroll" class="onlineusers">
            <ul>
    <?php
    // Create connection
    $conn = mysqli_connect($hostname_alliance, $username_alliance, $password_alliance, $database_alliance);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    ?>
    <?php
    $sql = "SELECT * FROM users_online JOIN members on users_online.userid = members.id WHERE members.id <> ".$id_user." ORDER BY cognome ASC";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
        $uId = 'u-'.$row['id'];
            ?>
        
                <li id="u-<?php echo $uId;?>" class="">
                            <div style="border:1px solid #ccc;width:25px;float:left;margin:1px;">
                                <div style="height:25px;overflow:hidden;">
                                    <img style=" width:25px" src="<?php echo $row['foto_profilo'];?>" />
                                </div>
                            </div>
                <span><?php echo strtoupper($row['cognome']);?> <?php echo strtoupper($row['nome']);?></span>
                </li>
    <?php 
        }
    }
    $conn->close();
    ?>                         
            </ul>
        </div><!--onlineusers-->
    </div><!--onlineuserpanel-->
    
    <script type="text/javascript">
    
        
            jQuery("#u-90").addClass("on");
       
    
    
        jQuery(document).ready(function(){
    
                    
    
    //                jQuery.ajax({
    //                    url: '../php/chat/stream_users.php',
    //                    type: 'POST',
    //                    dataType: 'json',
    //                    success: function( payload ){
    //                            jQuery.each( payload.data, function(i,msg){
    //                                jQuery( 'ul li#u-'+ msg.id_user).removeClass('on off').addClass(msg.online);
    //                        });
    //                    }
    //                    });
    //                setInterval(function(){ 
    //                jQuery.ajax({
    //                    url: '../php/chat/stream_users.php',
    //                    type: 'POST',
    //                    dataType: 'json',
    //                    success: function( payload ){
    //                            jQuery.each( payload.data, function(i,msg){
    //                                jQuery( 'ul li#u-'+ msg.id_user).removeClass('on off').addClass(msg.online);
    //                        });
    //                    }
    //                });
    //                },60000);
    
    
            // chat slim scroll
            if(jQuery('#chatscroll').length > 0) {
               
                jQuery('#chatscroll').slimscroll({
                    color: '#999',
                    size: '15px',
                    width: '200px',
                    height: '100%'
                });
               
                jQuery('#chatscroll li').click(function() {
                    
                    // store user ids in a cookie
                    var i = jQuery(this).attr('id');
                    if(jQuery.cookie('user-selected')) {
                        var c = jQuery.cookie('user-selected').split(',');
                        
                        // we make sure that what we inserted is not yet existing
                        var ue = false;
                        for(a = 0; a < c.length; a++) {
                            if(c[a] == i) ue = true;
                        }
                        
                        if(!ue) {
                            c.push(i);
                            jQuery.cookie('user-selected', c.join(','), { path: '/' });
                        }
                        
                    } else {
                        jQuery.cookie('user-selected', i, { path: '/' });
                    }
                
                    var n = jQuery(this).find('span').text();
                    var id = jQuery(this).attr('id');
                    var userxist = checkUser(id);
                      
                    var wincount = jQuery('#chatwindows .chatwin').length;
                    if(wincount < 4) {
                        
                        // check if user already opened a window
                        if(userxist == false) {
                            var chatwin = appendUser(n,id);
                            jQuery('#chatwindows').append(chatwin);
                        }
                         
                    } else {
                         
                        if(jQuery('#chatwinlist').length > 0) {
                            
                            if(!userxist) {
                                jQuery('#chatwinlist ul').append('<li id="c_'+ id +'"><h4>'+ n +'</h4></li>').show();
                                jQuery('#chatwinlist i').text(jQuery('#chatwinlist li').length);
                            }
                            
                        } else {
                            
                            if(!userxist) {
                                jQuery('#chatwindows').append('<div id="chatwinlist"><span class="iconfa-comment"></span><i>1</i></div>');
                                jQuery('#chatwinlist').append('<ul><li id="'+ id +'"><h4>'+ n +'</h4></li></ul>');
                            }
                            
                        }
                         
                    }
                }); // end of #chatscroll li click
                
                   
                jQuery('.chatwin .close').live('click', function(){
                      
                    var p = jQuery(this).parent().remove();
                        
                    if(jQuery('#chatwinlist li').length > 0) {
                        
                      var n = jQuery('#chatwinlist li:last-child h4').text();
                      var id = jQuery('#chatwinlist li:last-child').attr('id');
                      var chatwin = appendUser(n,id, false);
                      
                      jQuery(chatwin).insertBefore('#chatwinlist');
                      jQuery('#chatwinlist li:last-child').remove();
                      
                      jQuery('#chatwinlist i').text(jQuery('#chatwinlist li').length);
                      
                    } 
                    
                    if(jQuery('#chatwinlist li').length == 0)
                        jQuery('#chatwinlist').remove();
                        
                    // remove from cookies
                    var md = jQuery(this).parent().attr('id').split('_');
                    if(jQuery.cookie('user-selected')) {
                        
                        var nc = [];
                        var oc = jQuery.cookie('user-selected').split(',');
                        for(a = 0; a < oc.length; a++) {
                            if(oc[a] != md[1]) nc.push(oc[a]);
                        }
                        jQuery.cookie('user-selected', nc.join(','), { path: '/' });
                    }
                    
                    
                });
                
                jQuery('#chatwinlist span').live('click', function(){
                    var p = jQuery(this).parent().find('ul');
                    if(p.is(':visible')) p.hide(); else p.show();
                });
                
                
                jQuery('#chatwinlist li').live('click', function(){
                    
                    var n = jQuery(this).find('h4').text();
                    var id = jQuery(this).attr('id');
                    var chatwin = appendUser(n,id);
                    var cu = jQuery('.chatwin:nth-child(4) h4').text();
                    
                    jQuery(this).remove();
                    jQuery('#chatwinlist ul').append('<li><h4>'+ cu +'</h4></li>');
                    
                    jQuery(chatwin).insertBefore('#chatwinlist');
                    jQuery('.chatwin:nth-child(4)').remove();
                    
                });
                
                
                if(jQuery.cookie('user-selected')) {
                    var uc = jQuery.cookie('user-selected').split(',');
                    
                    var maxwin = 4;
                    if(jQuery(document).width() < 769) maxwin = 2;
                    if(jQuery(document).width() < 641) maxwin = 1;
                    //alert(jQuery(document).width());
                    
                    for(a = 0; a < uc.length; a++) {
                        var n = jQuery('#'+uc[a]).find('span').text();
                        var id = jQuery('#'+uc[a]).attr('id');
                        var userxist = checkUser(id);
                        if(a < maxwin) {
                            if(!userxist) {
                                var chatwin = appendUser(n,id,false);
                                jQuery('#chatwindows').append(chatwin);
                            }
                            
                        } else {
                            
                            if(jQuery('#chatwinlist').length > 0) {
                            
                                if(!userxist) {
                                    jQuery('#chatwinlist ul').append('<li id="'+ id +'"><h4>'+ n +'</h4></li>');
                                    jQuery('#chatwinlist').find('i').text(jQuery('#chatwinlist ul li').length);
                                }
                            
                            } else {
                            
                                if(!userxist) {
                                    jQuery('#chatwindows').append('<div id="chatwinlist"><span class="iconfa-comment"></span><i>1</i></div>');
                                    jQuery('#chatwinlist').append('<ul><li id="cu-'+ id +'"><h4>'+ n +'</h4></li></ul>');
                                }
                            
                            }
                        }
                    }
                }
                
                
                jQuery('.chatinput').live('keypress', function(e){
                    var t = jQuery(this);
                    var v = t.val();
                    if(e.which == 13) {
                        t.val('');
                        t.parents('.chatwin').find('.chatmsg').append('<li><strong>You:</strong> '+ v +'</li>');
                    }
                });
                
               
            } // end of slimscroll length
            
            function checkUser(id) {
                    
                userxist = false;
                jQuery('#chatwindows h4').each(function(){
                    var i = jQuery(this).parent().attr('id').split('_');
                    if(id == i[1]) {
                        userxist = true;
                        var p = jQuery(this).parent();
                        p.removeClass('animate0 bounceInUp')
                        .addClass('animate0 bounce')
                        .on('webkitAnimationEnd oanimationend oAnimationEnd msAnimationEnd animationend',function(){
                            p.removeClass('animate0 bounce');    
                        });
                    }
                });
                return userxist;
            }
                
            function appendUser(n,id,animate) {
                
                var a = (animate == null || animate == true)? 'animate0 bounceInUp' : '';
                    
                var chatwin = '<div id="c_'+id+'" class="chatwin '+ a +'">'
                            + '<a class="close">&times;</a>'
                            + '<h4>'+ n +'</h4>'
                            + '<ul class="chatmsg"></ul>'
                            + '<div class="chattext">'
                            + '<input name="text" class="form-control chatinput input-block-level" placeholder="Scrivi un messaggio e premi invio..." />'
                            + '</div><!--chattext-->'
                            + '</div><!--chatwin-->';
                       
                return chatwin;
                    
            }
            
        });
    </script>
    questo invece è il link alla pagina:
    http://allianceplus.altervista.org/chat.php

    dove cliccando sul link 'visualizza la chat in tutte le pagine', compare l'elenco incriminato.
    Confido nella vostra scienza
    Ultima modifica di maliba; 28-01-2015 a 13:06

  6. #6
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    io quell' "elenco incriminato" non lo vedo proprio, dovrebbe camparire nella parte destra?

  7. #7
    si, scusami.
    questo il link per l'accesso:
    http://allianceplus.altervista.org/index.php

    username: prova@prova.it
    pw: prova

    dopo aver fatto il login (per un fatto di sessioni) dovresti visualizzare tutto correttamente all'indirizzo:
    http://allianceplus.altervista.org/chat.php

  8. #8
    ho il sospetto che tutto dipenda da questo.
    l'elenco degli iscritti viene caricato dinamicamente con questo codice:

    codice:
            jQuery('body').addClass('chatenabled');
            jQuery.get('ajax/chat.php',function(data)
                {
                jQuery('body').append(data);
            });
    dipende forse da questo?
    ho provato ad inserire addClass dopo l'append ma neanche così va... sono veramente disperatissimo

  9. #9
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    ciao mi sembra che il problema siano gli id, nella pagina html sono cosi u-u-n mentre nel js u-n

  10. #10
    infatti. Era proprio questo il problema. Grazie mille

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.