检查元素是否具有纯JS的背景图像? [英] Check if an element has a background-image with pure JS?
本文介绍了检查元素是否具有纯JS的背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用纯Java技术检查特定元素是否具有与背景图像
相关联的正确方法是什么?
What is the correct way to check if a particular element has a background-image
associated with it, in pure Javascript?
现在我有这个:
var elementComputedStyle = window.getComputedStyle(element);
var hasBGImage = elementComputedStyle.getPropertyValue('background-image') !== 'none'
推荐答案
您所拥有的有效,但还有其他查找属性的方法,您可能会更容易找到它们。但是,我不相信有一种正确的方法。
What you have works, but there are alternate ways of finding the property that you might find easier. I don't believe there is a single 'correct' way to do it, however.
只是javascript:
Just javascript:
var hasBGImage = element.style.backgroundImage !== '';
使用jQuery:
var hasBGImage = $(element).css('background-image') !== 'none';
这篇关于检查元素是否具有纯JS的背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文