2012-04-20 71 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的文檔: