我正在使用Modernizr为创建一个备选方案。解决方案是有效的,但它会将代码应用于所有浏览器,而不仅仅是那些不支持的浏览器(如IE)。这是为什么呢?
我的代码:
无论我检查哪个浏览器,它总是记录:
Modernizr已正确包含和加载(即使我将本地文件更改为CDN路径,我也会得到相同的结果):
我的代码:
if ( ! Modernizr.objectfit ) {
console.log('object fit is not supported');
tpj('.featuredpost').each(function () {
var $container = tpj(this),
imgUrl = $container.find('.img-responsive').prop('src');
if (imgUrl) {
$container
.css('backgroundImage', 'url(' + imgUrl + ')')
.addClass('compat-object-fit');
}
});
tpj('.big-post').each(function () {
var $container = tpj(this),
imgUrl = $container.find('.img-responsive').prop('src');
if (imgUrl) {
$container
.css('backgroundImage', 'url(' + imgUrl + ')')
.addClass('compat-object-fit');
}
});
}else{
console.log('object is supported');
}
无论我检查哪个浏览器,它总是记录:
object fit is not supported
,即使在支持object-fit
的浏览器上也是如此。 我该怎么办?Modernizr已正确包含和加载(即使我将本地文件更改为CDN路径,我也会得到相同的结果):
<!-- Modernizr Library -->
<script type="text/javascript" src="js/modernizr.min.js"></script>