Javascript 是否有JQuery函数为所选元素提供与另一个元素相同的CSS?

Javascript 是否有JQuery函数为所选元素提供与另一个元素相同的CSS?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想知道是否有一个JQuery函数可以用来将CSS从一个给定元素应用到另一个元素。我说的是精确的CSS;由于继承,没有更多应用 更具体地说: <div id="d1"> <p id="p1">Here is some text.</p> </div> <div id="d2"> <p id="p2">Here is some other text.</p> <!-- I want

我想知道是否有一个JQuery函数可以用来将CSS从一个给定元素应用到另一个元素。我说的是精确的CSS;由于继承,没有更多应用

更具体地说:

<div id="d1">
    <p id="p1">Here is some text.</p>
</div>

<div id="d2">
    <p id="p2">Here is some other text.</p>
    <!-- I want this text in p2 to have the same styling as the text in p1, even after the inheritances of the divs are factored. -->
</div>

<script type="text/javascript">
    $(...).(...); // ??????????????
</script>

以下是一些文本

下面是一些其他文本

$(...).(...); // ??????????????

如果
JQuery
有这样一个功能,它将节省我一个小时的时间尝试使用我自己的样式表(我使用的是一个CMS,它有一个主样式表,我无法更改),以确定如何覆盖/添加一百万个属性。

虽然与Opera不兼容,但这应该可以实现您的目标:

  • 使用
    getComputedStyle
    获取目标元素的所有CSS属性
  • 将对象字符串化为CSS
  • 将cssText应用于接收元素
以下是相关代码:

var target = document.querySelector('#p1');
var receiver = document.querySelector('#p2');
var styles   = getComputedStyle(target);

var cssText = '';
for(var style in styles){
    cssText += style+':'+styles[style];
}

receiver.style.cssText = cssText;

不,JQuery没有这样的功能。我想他们就是为了这个目的发明了类:-)