Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript] Sprawdzanie klasy w divie od danym id
Forum PHP.pl > Forum > Przedszkole
GiboneKPL
Witam. Mam pewien problem. Czy jest możliwość sprawdzenia czy istnieje dany div od np. id="blokat" oraz w tym samym divie klasa class="blocka0"?

Coś takiego:
<div id="blokat" class="blocka0">...</div>

Ponieważ mam pewien problem. Robię ostatnie posty i tematy użytkownika na jednym z forów. I gdy otworzymy zakładkę posty i potem klikniemy na tematy to to się powiela.. Dlatego potrzebuje sprawdzenia.. Stworzyłem coś takiego:
Kod
        <script type="text/javascript">
            function getThreads()
            {
                if(jQuery("#blokat2")[0])
                {
                    document.getElementById('threads').className='bthreads2';
                }
                else
                {
                    document.getElementById('threads').className='bthreads1';
                }
            }    

            function getThreads2()
            {
                if(jQuery("#blokat")[0])
                {
                    document.getElementById('threads').className='bthreads2';
                }
                else
                {
                    document.getElementById('threads').className='bthreads1';
                }
            }
            
            function getPosts()
            {
                if(jQuery("#blokab")[0])
                {
                    document.getElementById('posts').className='bposts2';
                }
                else
                {
                    document.getElementById('posts').className='bposts1';
                }
            }    

            function getPosts2()
            {
                if(jQuery("#blokat2")[0])
                {
                    document.getElementById('posts').className='bposts2';
                }
                else
                {
                    document.getElementById('posts').className='bposts1';
                }
            }
        </script>


I potem buttony:
Kod
                        <a style="display: table-cell;font-size:12px;color:#fff;">
                            <div id="blokab" class="profilk_gg" onClick="document.getElementById('profil4').className='profil4_2'; document.getElementById('profile').className='profil4_4'; document.getElementById('posts').className='bposts2'; document.getElementById('blokab').className='profilk_gg blocka0'; document.getElementById('blokat').className='profilk_gg'; getThreads(); ">Posty</div>
                            <div id="blokat" class="blocka0" onClick="document.getElementById('profil4').className='profil4_1'; document.getElementById('profile').className='profil4_3'; document.getElementById('posts').className='bposts1'; document.getElementById('blokab').className='profilk_gg'; document.getElementById('blokat').className='profilk_gg blocka0'; getThreads2();">Posty</div
                        </a>

                        <a style="display: table-cell;font-size:12px;color:#fff;">
                            <div id="blokab2" class="profilk_gg" onClick="document.getElementById('profil4').className='profil4_2'; document.getElementById('profile').className='profil4_4'; document.getElementById('threads').className='bthreads2'; document.getElementById('blokab2').className='profilk_gg blocka0'; document.getElementById('blokat2').className='profilk_gg';getPosts();"> Tematy</div>
                            <div id="blokat2" class="blocka0" onClick="document.getElementById('profil4').className='profil4_1'; document.getElementById('profile').className='profil4_3'; document.getElementById('threads').className='bthreads1'; document.getElementById('blokab2').className='profilk_gg'; document.getElementById('blokat2').className='profilk_gg blocka0';getPosts2() "> Tematy</div>
                        </a>


Wiem, strasznie to pogmatwane i nie ogarniam biggrin.gif Pozdrawiam i czekam na pomoc smile.gif
markuz
Kod
Czy jest możliwość sprawdzenia czy istnieje dany div od np. id="blokat" oraz w tym samym divie klasa class="blocka0

[JAVASCRIPT] pobierz, plaintext
  1. if($('#blokat.blocka0').length > 0) console.log('istnieje');
[JAVASCRIPT] pobierz, plaintext
trueblue
https://gist.github.com/jjmu15/8646098
Czyli u Ciebie:
  1. if(hasClass(document.getElementById('blokat'), "blocka0"))
PrinceOfPersia
W jQuery jest już gotowa funkcja hasClass, tak samo hash(ten "krzyżyk": #) przed nazwa oznacza element o danym ID, więc znowu - document.getElementById('threads') robi to samo, co jQuery('#threads').

Innymi słowy robisz na piechotę to, co już ci załatwia jQuery.

Cytat
<div id="blokab" class="profilk_gg" onClick="document.getElementById('profil4').className='profil4_2'; document.getElementById('profile').className='profil4_4'; document.getElementById('posts').className='bposts2'; document.getElementById('blokab').className='profilk_gg blocka0'; document.getElementById('blokat').className='profilk_gg'; getThreads(); ">Posty</div>
<div id="blokat" class="blocka0" onClick="document.getElementById('profil4').className='profil4_1'; document.getElementById('profile').className='profil4_3'; document.getElementById('posts').className='bposts1'; document.getElementById('blokab').className='profilk_gg'; document.getElementById('blokat').className='profilk_gg blocka0'; getThreads2();">Posty</div

nie wrzucałbym publicznie takich kwiatków, bo ten kod to jedno wielkie WTF...
Pyton_000
A jakbym Ci powiedział że widziałem taki kwiatek tj. ten kod co pokazałeś 2x wrzucony w onclick dla div i takich div jest 7 wink.gif
PrinceOfPersia
to pomyślałbym chyba, że został on wygenerowany z automatu przez jakiś transkompilator do JavaScripta, albo przez narzędzie WYSIWYG. Ewentualnie przepuszczony przez narzędzie do obfuskacji kodu.
oneeyedsmiley02.png
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.