图像标记style.height未覆盖css类高度



Css

.myStyle {
height: 10px;
background-image: url(../myImage.png);
}

html

<img class=myStyle src=<%scriptlet%> >

现在问题出在我的js中,我为这个img标记编写了一个错误处理程序,它将高度设置为0px。此0px作为style.height=0px提供,但不覆盖类myStyle的高度。

js

myImage = $('.myStyle', $el);
myImage.error(function () {
           myImage.attr('style.height', '0px');
});

更改:

myImage.attr('style.height', '0px');

myImage.css('height', '0');

要获取背景图像,可以使用getting,如

myImage.css("background");

myImage.css("backgroundImage")

使用此:

myImage.height(0);

代替:

myImage.attr('style.height', '0px');

attr()是为属性操作而创建的,因此为了稍后的声明能够工作,您也可以将其更改为:

myImage.attr('style', 'height: 0px;');

请参阅jQuery文档中的attr()

改为使用这个:

myImage.height('10px');
myImage.style.height = '0px';

这也会起作用。

最新更新