Get Element StyleSheet Style in JavaScript

前端 未结 2 1769
谎友^
谎友^ 2020-12-04 02:40

I\'ve been using John Resig\'s getStyle function from Pro JavaScript Techniques to get the style of elements:

function getStyle(elem, name) {
           


        
相关标签:
2条回答
  • 2020-12-04 02:41

    Is it possible to get only the stylesheet specified styles of an element (and return null if the style is undefined)?

    That's effectively what is done by the routine you present. The problem is, in most scenarios, most styles are not undefined - they're inherited and/or defined by the individual browser's internal stylesheet.

    You could, with a whole lot of effort, iterate through all of the rules defining the style in question, in all of the stylesheets in the current view of the document, evaluate them for the element in question, and if none applied... and if none applied to a parent (or this particular style is not inherited)... then consider it undefined. This would be slow, and incredibly error-prone. I would not recommend trying it.

    Perhaps you would do better to step back and ask why you would ever need such a thing?

    0 讨论(0)
  • 2020-12-04 02:49

    Maybe your component can wrap the styles that it controls? Then when a style is set through the component the component knows what the user wants.

    0 讨论(0)
提交回复
热议问题