[英]JQuery doesn't replacing background image
I have a background image
on the header section of my blog.我的博客的 header 部分有一张
background image
。 I use a conditional php rule to check if the category is a podcast, make x
.我使用有条件的 php 规则来检查类别是否是播客,
make x
。 That work's, but the script to change the background image inside didn't work.那是可行的,但是更改内部背景图像的脚本不起作用。
if ( in_category( 14 ) ) : ?>
<script>
$('.page-title-section')
.css({ "background-image": "https://miradasocial.es/wp-content/uploads/2019/11/miradas-transp-largo-x3-300x115.png"});
</script>
<?php endif;
I tried with !important on the atribute and with $('.bg-secondary')
too, but this didn't work.我在属性上尝试了 !important 并且也尝试了
$('.bg-secondary')
,但这不起作用。
HTML Structure HTML结构
<section class="page-title-section bg-secondary"
style="background-image: url('https://miradasocial.es/wp-content/uploads/2019/11/entrevista-terraza.jpg')">
You can see live example here .您可以在此处查看实时示例。
The returned console error is Uncaught ReferenceError: $ is not defined
.返回的控制台错误是
Uncaught ReferenceError: $ is not defined
。
There's no need for Jquery if you're using PHP (laravel or anything) you can set you credential directly with PHP如果您使用的是 PHP (laravel 或任何东西),则不需要 Jquery ,您可以直接使用 PHP 设置您的凭据
<?php $pageTitleBgImage = in_category( 14 )
? "https://miradasocial.es/wp-content/uploads/2019/11/miradas-transp-largo-x3-300x115.png"
: "https://miradasocial.es/wp-content/uploads/2019/11/entrevista-terraza.jpg";
?>
<section
class="page-title-section bg-secondary"
style="background-image: url('<?php echo $pageTitleBgImage; ?>' )">
</section>
Please try like this.请像这样尝试。 you may need to add this code in templates or in footer
您可能需要在模板或页脚中添加此代码
if ( in_category( 14 ) ) : ?>
<script>
jQuery(document).ready(function($){
$('.page-title-section').css({ "background-image": "url(https://miradasocial.es/wp-content/uploads/2019/11/miradas-transp-largo-x3-300x115.png)"});
});
</script>
<?php endif;
Just tested its working.刚刚测试了它的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.