Javascript-форум (https://javascript.ru/forum/)
-   Events/DOM/Window (https://javascript.ru/forum/events/)
-   -   проблема с window.getComputedStyle (https://javascript.ru/forum/events/61819-problema-s-window-getcomputedstyle.html)

Gooody29 10.03.2016 13:29

проблема с window.getComputedStyle
 
Дорое время суток!
вопрос к гуру javascript :help:
есть "код":

<style>
	a { color:rgb(241, 105, 105); }
	a:hover { color:rgb(1, 105, 205); }
</style>
<h1>
	<a href="#" onclick="go(this);">click me and remove the mouse</a>
</h1>
<script>
function go(el)
{
     setTimeout(function()
     {
			var pseudoElt = '::hover';
			//var pseudoElt = ':hover';
			//var pseudoElt = 'hover';
			var elementStyle =  window.getComputedStyle ? window.getComputedStyle(el, pseudoElt) : el.currentStyle;
			console.log('Hover Result: ' + elementStyle.color);
     }
     , 2000);
    var pseudoElt = null;
    var elementStyle =  window.getComputedStyle ? window.getComputedStyle(el, pseudoElt) : el.currentStyle;
    console.log('Not Hover Result: ' + elementStyle.color);
}
</script>



я кликаю по ссылке и убираю курсор мышки, в результате в консоле получаю:
Not Hover Result: rgb(1, 105, 205)
Hover Result: rgb(241, 105, 105)

Как мне получить правильные данные для "hover" и не "hover", не используя setTimeout и вне зависимости от наведения курсора?

destus 10.03.2016 13:42

Gooody29,
https://developer.mozilla.org/en-US/...tComputedStyle. Вторым параметром передается псевдоэлемент (:after, :before), а не псевдокласс (:hover)

destus 10.03.2016 13:45

Gooody29,
<style>
	a { color:rgb(241, 105, 105); }
	a:hover { color:rgb(1, 105, 205); }
</style>
<h1>
	<a href="#">click me and remove the mouse</a>
</h1>
	<script>
	var i,j, sel = /a:hover/;
for(i = 0; i < document.styleSheets.length; ++i){
    for(j = 0; j < document.styleSheets[i].cssRules.length; ++j){    
        if(sel.test(document.styleSheets[i].cssRules[j].selectorText)){
                alert(document.styleSheets[i].cssRules[j].style.cssText)
        }
    }
}
	</script>


Часовой пояс GMT +3, время: 09:10.