Created
July 27, 2015 21:52
-
-
Save mishelen/0dcacfb82e0e765bb921 to your computer and use it in GitHub Desktop.
Мы не можем получить прямого доступа к этим элементам, так как они не создают узел DOM.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Самое простое менять класс контейнера для псевдоэлемента, с предопределенными стилями и конентом | |
| $('p').on('click', function() { | |
| $(this).toggleClass('special'); | |
| }); | |
| // Чтобы их динамично менять, | |
| // можно только получить доступ к их контейнеру с его свойством content, | |
| // которое должно быть предопределено в том же CSS. | |
| // А потом мы просто задаем соответстующий атрибут элементу | |
| /* p:before { | |
| content: attr(data-before); | |
| color: red; | |
| cursor: pointer; | |
| } */ | |
| // JS: | |
| $('p').on('click', function () { | |
| $(this).attr('data-before','bar'); | |
| }); | |
| // Только стили псевдоэлемента так менять не получится. | |
| // ТОгда мы можем генерировать правила для css. | |
| var str = "bar"; | |
| document.styleSheets[0].addRule('p.special:before','content: "'+str+'";'); | |
| // или вообще так | |
| $('head').append("<style>.span::after{ content:'bar' }</style>"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var color = window.getComputedStyle( | |
| document.querySelector('.element'), ':before' | |
| ).getPropertyValue('color') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment