Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Pobieranie danych, z zewnetrznego css-a
kicaj
post 28.04.2006, 19:19:40
Post #1





Grupa: Zarejestrowani
Postów: 1 640
Pomógł: 28
Dołączył: 13.02.2003
Skąd: Międzyrzecz/Poznań

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


Jak pobrac dane (np. kolor, rozmiar) ze styli w zewnetrznym pliku css?


--------------------
PHP Developer

"Nadmiar wiedzy jest równie szkodliwy jak jej brak" Émile Zola
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 5)
akubiczek
post 28.04.2006, 22:12:28
Post #2





Grupa: Zarejestrowani
Postów: 189
Pomógł: 0
Dołączył: 4.07.2004
Skąd: z neostrady

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


Jeżeli masz już zaimportowany stylesheet, np. pierwszy:

document.styleSheets[0].cssRules[index]

- zobacz sobie w DOM Inspector jaka jest struktura

Ten post edytował akubiczek 28.04.2006, 22:13:05
Go to the top of the page
+Quote Post
kicaj
post 28.04.2006, 22:32:40
Post #3





Grupa: Zarejestrowani
Postów: 1 640
Pomógł: 28
Dołączył: 13.02.2003
Skąd: Międzyrzecz/Poznań

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


Cytat(akubiczek @ 2006-04-28 23:12:28)
document.styleSheets[0].cssRules[index]

Wlasnie doszedlem do tego tez:
Kod
document.styleSheets[0].cssRules[index].style.fontSize;

Ale z tego co widze zamiast `index` nie mozna wpisac nazwy, tylko odwolywac sie trzeba poprzez liczby - co mnie nie zadowala...


--------------------
PHP Developer

"Nadmiar wiedzy jest równie szkodliwy jak jej brak" Émile Zola
Go to the top of the page
+Quote Post
Cysiaczek
post 25.08.2006, 14:21:08
Post #4





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Przyłączam się powyższego postu - ja pobrac regułę po jej nazwie?

Dzikeuję i pozdrawiam,


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post
revyag
post 25.08.2006, 17:21:21
Post #5





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

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


Przykładowy css:
Kod
* {
    margin: 0;
    padding: 0;
}
body {
    text-align: center;
    font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}
#cont {
    margin: 0 auto;
    background-color: black;
    color:#ffffff;
}
#head {
    color:green;
}
.foot {
    color:red;
}


js:
Kod
function getrule(rule,attr) {
    
    var rules = document.styleSheets[0].cssRules;
    var ct = rules.length;
    var out = '';
    var arr = new Array();
    
    if(attr.indexOf('-') > -1) {
        arr = attr.split("-");
        attr = arr[0].concat(arr[1].slice(0,1).toUpperCase().concat(arr[1].substr(1)));
    }
    
    for(var i = 0; i < ct; i++) {
        if(rules[i].selectorText == rule) {
            out = rules[i].style[attr];    
        }
    }
    
    alert(out);
}


przykład:
  1. <button onclick="getrule('#cont','background-color')">get</button>


--------------------
-------------

------
Go to the top of the page
+Quote Post
Cysiaczek
post 25.08.2006, 17:28:31
Post #6





Grupa: Moderatorzy
Postów: 4 465
Pomógł: 137
Dołączył: 26.03.2004
Skąd: Gorzów Wlkp.




Dzięki za odpowiedź laugh.gif . Ja też pogoglowałem trochę i znalazłem taki przykład:

  1. var styleSheets = document.styleSheets;
  2. for (var i = 0; i < styleSheets.length; i++) {
  3. var rules = styleSheets[i].rules;
  4. for (var j = 0; j < rules.length; j++) {
  5. if (rules[j].selectorText ==
  6. '#nazwa') {
  7. //cos robimy
  8. return true;
  9. }
  10. }
  11. }


Szkoda, ze nie uwzględnili jakiejś prostszej metody na dostęp do tych css'ów :/

Pozdrawiam.


--------------------
To think for yourself you must question authority and
learn how to put yourself in a state of vulnerable, open-mindedness;
chaotic, confused, vulnerability, to inform yourself.
Think for yourself. Question authority.
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 04:49