jQuery get css hover values

落花浮王杯 提交于 2019-12-06 03:17:57

问题


On page ready I want to run a function that will get the css background-image setting and also the hover background-image setting which I wish to then utilize in another fashion.

The only method I have seen to accomplish this requires someone to actually mouse over the element before you can get the value.

Anyone have a way?

This is how to get a css non-hover value:

$(this).css('background-color');

回答1:


To do this you will need to read values from the stylesheet directly. The basic idea is to iterate through each stylesheet in document.styleSheets, and within these, iterate through each rule in .cssRules or .rules, looking for a .selectorText that matches what you're after.

To simplify things, I wrote a (tiny) library for this purpose - JSS. If you have a selector, for example #someId:hover, then using JSS, you can get its background-color property with:

jss('#someId:hover').get()['background-color']


来源:https://stackoverflow.com/questions/5319472/jquery-get-css-hover-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!