gpt4 book ai didi

jquery - ToggleClass 如果 body 有 css 内联样式

转载 作者:太空宇宙 更新时间:2023-11-04 07:35:12 24 4
gpt4 key购买 nike

我有一个使用此代码的页面:

<body style="background-image:url("http://...")">
<div class="g1-body-inner"></div>
</div>

还有其他没有体型的人。

使用 Jquery,我希望仅当 body 将背景图像作为内联样式时,才向 div 添加一个新类。

我已经搜索并尝试过,但没有任何效果......无论我尝试什么代码,结果总是正确的(添加了 toggleclass)。

我试过以下代码:

jQuery(document).ready(function( $ ){
if ($('body').css('background-image').length != null) {
$('.g1-body-inner').toggleClass("anuncio-cheio");
}
});

jQuery(document).ready(function( $ ){
var $el = $('body[style*="background-image"]');
if ( $el.length > 0 ) {
$('.g1-body-inner').toggleClass("anuncio-cheio");
}
});

jQuery(document).ready(function( $ ){
if ($( "body[style!='background-image']" )) {
$('.g1-body-inner').toggleClass("anuncio-cheio");
}
});

有人可以帮忙吗?

谢谢。

最佳答案

为什么不尝试使用一些字符串验证而不是像这样

var style = $('body').attr('style');

if(style){
if (style.search('background') > -1){
//It has background inline style
} else {
//It doesn't have background inline style
}

} else {
//It doesn't have inline style
}

关于jquery - ToggleClass 如果 body 有 css 内联样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49012589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com