how to get all the calculated styles of an element with jQuery?

前端 未结 2 1722
不思量自难忘°
不思量自难忘° 2020-12-15 08:23

I want to copy all the calculated css of an element and apply it to another element. By calculated style, I mean for examply if the element\'s width is 100%, but it\'s paren

相关标签:
2条回答
  • 2020-12-15 09:14

    after further research using Majid's answer, I found out that Jquery's ".css" method return computed style as well, and is better because it accounts for browser difference http://api.jquery.com/css/

    0 讨论(0)
  • 2020-12-15 09:18

    Use computed-style. Here's a simple tutorial: Get Computed Style

    And as for jquery, it can help you get a reference to the source and target element and apply the css rule easily. let's say you are only interested in two properties, width and height, then you can use code along the lines of:

    var oSource = $('#source');
    var sWidth  = document.defaultView.getComputedStyle(oSource, null).width;
    var sHeight = document.defaultView.getComputedStyle(oSource, null).height;
    $('#target').css('width', sWidth).css('height', sHeight);
    
    0 讨论(0)
提交回复
热议问题