2012-04-20 55 views
2

是否有可能获得元素的原始HTML,或者有可能得到没有“style”属性的HTML?考虑下面的例子,jquery获取元素的HTML剥离样式属性

<div class="outer"> 
    <div class="inner"> 
     some text 
    </div> 
</div> 

现在,我将一些动画/ CSS应用到“内部”元素。

​$('.inner').animate({ 
    'margin': '-10px' 
});​​ 

当我使用console.log($('.outer').html()); “外部” 元素的HTML,我得到以下

<div class="inner" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "> 
     some text 
    </div> 

但是,我确实需要这只是

<div class="inner"> 
     some text 
    </div> 

有什么简单的获得输出的方式?在应用animate()或css()之前,我不需要创建元素的备份。

谢谢。

+0

“当我得到的HTML ”外“ 元素”。你最近怎么样?你能发布你使用的代码吗? – j08691 2012-04-20 18:55:22

+0

尝试'$('。inner')。removeAttr(“style”);'删除样式 – mgraph 2012-04-20 18:56:30

回答

4

如果你想彻底删除样式属性(又不想回),使用下面的代码:

/* Working code */ 
$(".outer").find(".inner").removeAttr('style').end().html(); 

/* Explanation of working code */ 
$(".outer")      // Get the outer element 
      .find(".inner")  // Find the inner div 
      .removeAttr('style') // Remove the attribute from the inner div 
      .end()    // Return to the outer div 
      .html();    // Get outer div's HTML 

这里的a working fiddle

jQuery的文档: