繁体   English   中英

如何使用JQuery从img标记中删除内联样式?

[英]How to remove inline style from img tag using JQuery?

我已经尽一切努力从页面的<img>标记中删除内联样式。 没有任何效果。

给定呈现的html像:

<p><img alt="" src="../../../../images/Cuffs.jpg" style="width: 620px; height: 395px;" /></p>

如何从页面中的所有图像中删除<style>属性? 我已经尝试过removeAttr以及各种SO帖子中的代码,但没有任何效果。

编辑:

我试过了

<script>
    $(document).ready(function ($) {
        $('img').each(function () {
            $(this).removeAttr('style')   
        });
    });
</script>

和:

<script>
    $(document).ready(function ($) {
        $('img').removeAttr("style")
     });
</script>

<script>
    $(document).ready(function ($) {
        $('img').attr('style', '');
    });
</script>

以及网络上大约3或4个其他示例。

当我查看源代码时,我看到:

 <p><img alt="" src="../../../../images/Cuffs.jpg" style="width: 620px; height: 395px;" /</p>

检入Chrome

您的第一次尝试非常有效:

 $(document).ready(function ($) { $('img').each(function () { $(this).removeAttr('style') }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <p> <img alt="" src="http://lorempixel.com/32/32" style="width: 620px; height: 395px;" /> </p> 

$().removeAttr('style'); 适用于Safari,Chrome和Firefox(均为最新版本)。 不确定在IE上,但肯定可以。

$(function() {
   $('img').removeAttr("style");
});

看到这个jsBin: http ://jsbin.com/yokaxo/1/

使用.removeAttr()方法( 文档 )。

在我的情况下,图像已设置了“高度”和“宽度”属性,因此您可能需要执行以下操作,而不是定位“ style”属性:

$('img').each(function ()
{
    $(this).removeAttr('width');
    $(this).removeAttr('height');
});

当然,您也可以删除style属性以求完整性,但这取决于您。

$('img').each(function ()
{
    $(this).removeAttr('width');
    $(this).removeAttr('height');
    $(this).removeAttr('style');
});

无论如何,希望它能帮助某人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM