Jquery 函数,如果未加载图像,则执行 css 更改



我需要创建一个函数,如果图像加载失败,我的图像持有者元素会删除填充。

$("img").error(function(){
$(this).css({padding-right:"0px"});
});
.image-holder {
height: 100px;
width: auto;
display: block;
float: left;
top: -4px;
position: relative;
clear: both;
padding-right: 25px;
}
img {
height: 100px;
width: auto;
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="toggle-title">
<div class="image-holder"><img src="https://vignette.wikia.nocookie.net/arianagrande/images/7/70/Example.png/revision/latest?cb=20160301231046" alt="">
</div>

<div class="cfp-heading">
<p class="cfp-title">Journal Of Network Theory In Finance</p>
<p class="cfp-subtitle">Call for papers</p>
</div>
</div>

上面是我为尝试使其工作而编写的代码,但它不断左右和居中抛出错误。我要做的就是...

如果图像加载失败,请删除右填充(或添加右填充:0px;(。帮助将不胜感激

以下代码对我有用:

$(function(){
$("img").error(function(){
$(this).parent(".image-holder").css({"padding-right":"0px"});
});
})
$(this).css("padding-right","0");

最新更新