jQuery 如何删除背景图像属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6747176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:20:33  来源:igfitidea点击:

How can I remove a background-image attribute?

jquerycss

提问by markzzz

I have this code :

我有这个代码:

if (pansel.val() == "1") 
     $(#myDiv).css('background-image', 'url(/private_images/guida_check_category.jpg)'); 
else 
     $(#myDiv).css({ 'background-color': '#ffffff' });

and I see that, when I apply the background-image, the follow code :

我看到,当我应用背景图像时,以下代码:

$(#myDiv).css({ 'background-color': '#ffffff' });

doesnt remove the image and put the white background color. The image still present!

不删除图像并放置白色背景色。图片还在!

How can I totally remove that background-image attribute?

如何完全删除该背景图像属性?

回答by CdB

Try this:

尝试这个:

if (pansel.val() == "1") 
     $("#myDiv").css('background-image', 'url(/private_images/guida_check_category.jpg)'); 
else {
     $("#myDiv").css({ 'background-color': '#ffffff' });
     $("#myDiv").css('background-image', 'none');
}

回答by Noah Whitmore

This code...

这段代码...

$myDiv = $('#myDiv');
$myDiv.css('background-image', 'none');

will not actually removethe background-image property. It will make jQuery set an inline style of 'background-image:none' to the myDiv element, which may not be what you want.

实际上不会删除background-image 属性。它将使 jQuery 为 myDiv 元素设置内联样式“background-image:none”,这可能不是您想要的。

If you later apply a class to the myDiv element which uses a background image or attempt to use 'background: url('... to set a background image, the inline style will override and the background image will not show.

如果稍后将类应用于使用背景图像的 myDiv 元素或尝试使用 'background: url('... 来设置背景图像,则内联样式将覆盖并且背景图像不会显示。

To remove the property, I would suggest that you just set the 'background-image' value to nothing, then it will disappear from the inline style all together and you can manipulate it much more easily...

要删除该属性,我建议您只需将 'background-image' 值设置为空,然后它将一起从内联样式中消失,您可以更轻松地对其进行操作...

if (pansel.val() === "1") {
    $myDiv.css('background-image', 'url(/private_images/guida_check_category.jpg)'); 
} else {
    $myDiv.css('background-color', '#ffffff').css('background-image', ''); 
    //the value will be set to white with the image 'empty', so it will disappear from inline styles
}

回答by epoch

background:0will remove the entire background css

background:0将删除整个背景 css

回答by Alan Dong

$('*').css('background', 'transparent')

$('*').css('background', 'transparent')

see: Remove background color/images for all elements with jQuery

请参阅:使用 jQuery 删除所有元素的背景颜色/图像

回答by aggaton

If you want to completely remove the style attribute you can do something like this

如果你想完全删除 style 属性,你可以做这样的事情

var oDiv = document.getElementById("myDiv");
oDiv.style.removeProperty("background-color");

I have found that setting it to transparent creates some odd conflicts, if you later do other color operations on that area. Removing the attribute works better if you are trying to revert back to some default setting specified in your css.

我发现将其设置为透明会产​​生一些奇怪的冲突,如果您稍后对该区域进行其他颜色操作。如果您尝试恢复到 css 中指定的某些默认设置,则删除该属性效果会更好。

回答by Developer

Get, Set and Delete Div Background Image In jQuery

在 jQuery 中获取、设置和删除 Div 背景图像

Get Background image

获取背景图片

You can use the below code for getting the background image in jQuery.

您可以使用以下代码在 jQuery 中获取背景图像。

$(".btn-get").click(function() {
var bg = $('div').css('background-image'); // get background image using css property
bg = bg.replace('url(','').replace(')','');
alert(bg); });   

Set Background Image

设置背景图像

You can use the below code for setting the background image using the jQuery CSS property.

您可以使用以下代码使用 jQuery CSS 属性设置背景图像。

 $(".btn-set").click(function() {
 var img = 'http://www.tutsmake.com/wp-content/uploads/2019/08/How-to-Encode-Decode-a-URL-Using-JavaScript.jpeg';
 var bg = $('div').css('background-image',"url(" + img + ")");
});

Remove Background Image

删除背景图像

You can use the below code for removing the background image using the jQuery CSS property.

您可以使用以下代码使用 jQuery CSS 属性删除背景图像。

$(".btn-remove").click(function() {
    var bg = $('div').css('background-image','none');
});