Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript] Sprawdzanie klasy w divie od danym id
GiboneKPL
post
Post #1





Grupa: Zarejestrowani
Postów: 10
Pomógł: 0
Dołączył: 27.01.2014

Ostrzeżenie: (0%)
-----


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 (IMG:style_emoticons/default/biggrin.gif) Pozdrawiam i czekam na pomoc (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 5)
markuz
post
Post #2





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


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


Ten post edytował markuz 18.09.2014, 18:56:34
Go to the top of the page
+Quote Post
trueblue
post
Post #3





Grupa: Zarejestrowani
Postów: 6 809
Pomógł: 1828
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


https://gist.github.com/jjmu15/8646098
Czyli u Ciebie:
  1. if(hasClass(document.getElementById('blokat'), "blocka0"))
Go to the top of the page
+Quote Post
PrinceOfPersia
post
Post #4





Grupa: Zarejestrowani
Postów: 717
Pomógł: 120
Dołączył: 18.04.2009

Ostrzeżenie: (0%)
-----


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...

Ten post edytował PrinceOfPersia 18.09.2014, 19:45:10
Go to the top of the page
+Quote Post
Pyton_000
post
Post #5





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


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 (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
+Quote Post
PrinceOfPersia
post
Post #6





Grupa: Zarejestrowani
Postów: 717
Pomógł: 120
Dołączył: 18.04.2009

Ostrzeżenie: (0%)
-----


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.
(IMG:style_emoticons/default/oneeyedsmiley02.png)

Ten post edytował PrinceOfPersia 18.09.2014, 20:24:08
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.12.2025 - 00:14