jQuery选择并应用样式

jQuery选择并应用样式,jquery,Jquery,我有一张有很多td手机的桌子。每个单元格内可能有一个或多个div。我需要获取第一个内部div的内联样式,并将其移动到父td的样式 在jQuery中如何做到这一点?这非常简单: $('td div').each(function() { $(this).closest('td').attr('style', $(this).attr('style')); }); 这很简单: $('td div').each(function() { $(this).closest('td').attr(

我有一张有很多td手机的桌子。每个单元格内可能有一个或多个div。我需要获取第一个内部div的内联样式,并将其移动到父td的样式

在jQuery中如何做到这一点?

这非常简单:

$('td div').each(function() {
  $(this).closest('td').attr('style', $(this).attr('style'));
});
这很简单:

$('td div').each(function() {
  $(this).closest('td').attr('style', $(this).attr('style'));
});

要仅应用第一个div样式,请执行以下操作:

$('td div:first-child').each(function(){
    $(this).parent('td').attr('style', $(this).attr('style'));
});

要仅应用第一个div样式,请执行以下操作:

$('td div:first-child').each(function(){
    $(this).parent('td').attr('style', $(this).attr('style'));
});

我猜您希望得到的是computed样式,而不是div元素的显式样式参数。在StackOverflow中已经很少有问题试图找到答案了


  • 解决方案提供了两种处理方法:创建要复制的样式属性列表,并使用
    parentTd.css(property,childDiv.css(property))
    对其进行迭代,或者使用浏览器提供的computedStyle。不幸的是,Internet Explorer不支持后者,需要一种变通方法

    我猜您想要得到的是computed样式,而不是div元素的显式样式参数。在StackOverflow中已经很少有问题试图找到答案了


  • 解决方案提供了两种处理方法:创建要复制的样式属性列表,并使用
    parentTd.css(property,childDiv.css(property))
    对其进行迭代,或者使用浏览器提供的computedStyle。不幸的是,Internet Explorer不支持后者,需要一种变通方法

    应用“内联样式”是否足够好?您是指
    样式
    属性、类还是所有CSS属性?通过应用,您是指“同时应用”样式还是将样式从
    移动到其父级
    ?应用“内联样式”是否足够好?您是指
    样式
    属性,类或所有CSS属性?通过应用,您的意思是“同时应用”还是将样式从
    移动到其父级
    ?div已经有了需要传递给td的CSS样式。div已经有了需要传递给td的CSS样式。在内联样式澄清之前开始浏览。我将留下评论,以便其他人能够找到更复杂场景的解决方案。在解释内联样式之前开始浏览。我将留下评论,以便其他人能够找到更复杂场景的解决方案。