我有一个有趣的问题,我不明白。这是一个简单的jQuery if else语句,但它不起作用。为什么它不工作?
代码:
$("#div").click(function(){
if ( $("#div2").css("background-image") === "url(img/image.png)") {
alert('OK');
}
else {
alert('NG');
}
});但是,例如,当我将.css("background-image") === "url(img/image.png)"更改为.css('position') === 'absolute'时,它就起作用了。
发布于 2012-03-16 16:17:14
似乎正确的答案是写入镜像的完整路径。因此,请使用url(http://www.myhost.com/img/image.png)而不是url(img/image.png)。
发布于 2012-03-16 03:44:58
值中可能存在细微的差异;警告else语句中的值,以查看发生了什么。例如,
if ( $("#div2").css("background-image") === "url(img/image.png)") {
alert('OK');
}
else {
alert($("#div2").css("background-image"));
}发布于 2012-03-16 03:47:01
我的猜测是,当css加载时,它会将url(img/image.png)带到http://www.somehost.com/img/image.png中。因此,图像(“.css-image”)将不再等于"url(img/image.png)“。
而不是检查
.css('background-image') == 'url(img/Image.png)'检查
.css('background-image').indexOf("img/Image.png") != -1https://stackoverflow.com/questions/9726985
复制相似问题