[英]jquery get HTML of an element stripping the style attribute
是否可以获取元素的原始HTML或不具有“ style”属性的HTML? 考虑以下示例,
<div class="outer">
<div class="inner">
some text
</div>
</div>
现在,我将一些动画/ CSS应用于“内部”元素。
$('.inner').animate({
'margin': '-10px'
});
当我使用console.log($('.outer').html());
获得“外部”元素的console.log($('.outer').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()之前,不需要创建元素的备份。
谢谢。
如果要完全删除样式属性(并且不希望返回),请使用以下代码:
/* 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
这是工作中的小提琴 。
jQuery文档:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.