Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [CSS] nadanie wartości elementowi wyżej
lustfingers
post 9.11.2016, 05:37:15
Post #1





Grupa: Zarejestrowani
Postów: 99
Pomógł: 5
Dołączył: 18.03.2015

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


Witam mam taką strukturę kontenerów, i jest ich kilkanaście identycznych w układzie

  1. <li class="vir">
  2. <div class="radio">
  3. <span class=""><input type="radio" class="attribute_radio" name="group_4" value="34"></span>
  4. </div>
  5. </li>


Gdy jeden z nich zostanie zaznaczony do span dochodzi klasa checked:

  1. <li class="vir">
  2. <div class="radio">
  3. <span class="checked"><input type="radio" class="attribute_radio" name="group_4" value="34"></span>
  4. </div>
  5. </li>


Teraz dla tego właśnie kontenera <li class="vir"> w którym jest zaznaczony <span class="checked"> chce nadać inne tło. Czy jest to możliwe w css? Czy potrzebuje jakiś kod js który to obsłuży?
Go to the top of the page
+Quote Post
viking
post 9.11.2016, 06:43:05
Post #2





Grupa: Zarejestrowani
Postów: 6 378
Pomógł: 1116
Dołączył: 30.08.2006

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


W css nie jest to możliwe. W js musisz wyszukać element nadrzędny i go odpowiednio formatować.


--------------------
Go to the top of the page
+Quote Post
trueblue
post 9.11.2016, 08:45:56
Post #3





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

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


Zmień trochę strukturę i użyj: https://css-tricks.com/almanac/selectors/c/checked/


--------------------
Go to the top of the page
+Quote Post
lustfingers
post 9.11.2016, 09:08:22
Post #4





Grupa: Zarejestrowani
Postów: 99
Pomógł: 5
Dołączył: 18.03.2015

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


Generalnie nie za bardzo mogę zmienić układ, ogólnie ten span pojawia się automatycznie w kodzie, w samej strukturze pliku go nie ma więc jakiś skrypt go ładuje ale nie potrafie namierzyć gdzie on jest. Poza tym chyba jak bym namierzył to i tak w innym miejscu na stronie ta funcjonalność musi pozostać nie zmieniona, więc jak zjeśc ciastko i mieć ciastko tongue.gif

Za chwilę przeglądne link który podałeś, tymczasem przy okazji mógłby ktoś podesłać jakiś przykład js który pozwoli mi obsłużyć takie działanie, albo pod jaką nazwą szukać w googlu?

Ten post edytował lustfingers 9.11.2016, 09:09:29
Go to the top of the page
+Quote Post
viking
post 9.11.2016, 09:23:41
Post #5





Grupa: Zarejestrowani
Postów: 6 378
Pomógł: 1116
Dołączył: 30.08.2006

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


https://jsfiddle.net/ao4wwwh1/

Jeśli masz jakiś framework typu jquery to parents() / parent()


--------------------
Go to the top of the page
+Quote Post
lustfingers
post 9.11.2016, 19:21:59
Post #6





Grupa: Zarejestrowani
Postów: 99
Pomógł: 5
Dołączył: 18.03.2015

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


Kod który podałeś umieściłem tak:

  1. <script type="text/jscript">
  2. $(function() {
  3. var x =document.querySelectorAll('span.checked');
  4. [].forEach.call(x, function(span) {
  5. span.parentElement.parentElement.classList.add('test');
  6. });
  7. });


Podczas ładowania strony domyślnie jeden element ma klasę checked i tam faktycznie jest dopisywana klasa "test", natomiast gdy zaznaczę inny element to nie ma żadnego zdarzenia które by usuwało klasę z tego li i dodawało do nowo zaznaczonego.

EDYCJA

Dostałem kilka kodów które powinny działać ale nie działają, wydaje mi sie że mają coś z klasami namieszane ale nie mogę dojść z tym ładu,w oryginale te kody któe dostałem wyglądają tak:

  1. $(function() {
  2. $('span.checked input').change(function() {
  3. var x = document.querySelectorAll('span');
  4. [].forEach.call(x, function(span) {
  5. if (span.parentElement.parentElement.classList.contains('checked')) { // check if li has class
  6. span.parentElement.parentElement.classList.remove('checked'); // remove class test from li
  7. }
  8. });
  9. this.parentElement.parentElement.parentElement.classList.add('test');
  10. }).change(); // .change will run for the 1st time when page loads
  11. });


kolejny to:

  1. $(function() {
  2. $('span.checked input').change(function() {
  3. var x = document.querySelectorAll('li.vir');
  4. [].forEach.call(x, function(li) {
  5. if (li.contains('checked')) { // check if li has class
  6. li.classList.remove('checked'); // remove class test from li
  7. }
  8. });
  9. this.parentElement.parentElement.parentElement.classList.add('test');
  10. }).change(); // .change will run for the 1st time when page loads
  11. });


i trzeci:

  1. $(function() {
  2. $('span.checked input').change(function() {
  3. $('li.vir').removeClass('test'); //remove class test from li
  4. $(this).closest('li').addClass('test'); // add class test to li
  5. }).change(); // .change will run for the 1st time when page loads
  6. });




EDYCJA 2

Mam działające rozwiązania które u mnie nie działają. Pytanie dlaczego to może u mnie nie działać?

Tutaj dwa teoretycznie działające rozwiązania, które u mnie nie ruszają:
https://jsfiddle.net/ukbgqn1c/4/
https://jsfiddle.net/ukbgqn1c/3/

Ten post edytował lustfingers 9.11.2016, 19:30:25
Go to the top of the page
+Quote Post
viking
post 9.11.2016, 19:29:14
Post #7





Grupa: Zarejestrowani
Postów: 6 378
Pomógł: 1116
Dołączył: 30.08.2006

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


Zobacz w konsoli jakie masz błędy. I używasz jquery tak do wiadomości.


--------------------
Go to the top of the page
+Quote Post
lustfingers
post 9.11.2016, 19:48:40
Post #8





Grupa: Zarejestrowani
Postów: 99
Pomógł: 5
Dołączył: 18.03.2015

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


W konsoli nie mam błędów z tym związanych, jquery jest ładowane to jest PrestaShop i hederze są ładowane skrypty, tym bardziej ze to strona produktu więc musi być, teraz zmieniłem kod na:

  1. jQuery(document).ready(function() {
  2. $('span input:radio').change(function() {
  3. if (this.checked) { // if radio button is checked
  4. $('li.vir').removeClass('test'); //remove class test from li
  5. $(this).closest('li').addClass('test'); // add class test to li
  6. }
  7. }).change(); // .change will run for the 1st time when page loads
  8. });


więc tym bardziej w konsoli pokazało by mi że nie ma jquery.

Ten post edytował lustfingers 9.11.2016, 20:04:50
Go to the top of the page
+Quote Post
trueblue
post 9.11.2016, 21:27:25
Post #9





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

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


https://jsfiddle.net/ukbgqn1c/5/


--------------------
Go to the top of the page
+Quote Post
lustfingers
post 10.11.2016, 16:28:52
Post #10





Grupa: Zarejestrowani
Postów: 99
Pomógł: 5
Dołączył: 18.03.2015

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


To jest to, dzięki.
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: 14.07.2025 - 04:53