javascript 使用 jQuery 询问 <div> 是否有背景图片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3625741/
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-10-25 01:46:24  来源:igfitidea点击:

Use jQuery to ask if <div> has a background image?

javascriptjquery

提问by steffanjj

Is it possible to use jQuery to check if #pagehas got a background image - #pagecould look like this:

是否可以使用 jQuery 检查是否#page有背景图像 -#page可能如下所示:

<div id="page" style="background-image: url(xxx)"></div>

If it contains a background image, it should add a class to #page

如果它包含背景图像,则应向#page 添加一个类

回答by steffanjj

The default value for background-imageis "none". So you can do it like this:

的默认值为background-image“无”。所以你可以这样做:

if ($('#page').css('background-image') != 'none') {
  alert('There is a background image');
}

回答by pharalia

var image = $('#page').css('background-image');

Should do the job.

应该做这项工作。