简体   繁体   English

jQuery不显示背景图片

[英]jQuery doesn't display background image

I have a newbie question. 我有一个新手问题。

I am trying to display a background-image for an a-element. 我正在尝试显示a元素的背景图像。 On mouseover the picture should change to different picture. 在鼠标悬停时,图片应更改为其他图片。 The problem is that the pictures are not being displayed. 问题是未显示图片。 When I had the code in CSS it worked fine so it must be my function that makes problems. 当我使用CSS中的代码时,它可以很好地工作,所以一定是我的函数出了问题。

My code: 我的代码:

jQuery(document).ready(function($){
    $(document).ready(function() {
        $(".person").css({background-image: url('<?php echo get_stylesheet_directory_uri(); ?>/img/person.jpg')});
        $(".person").mouseover(function() {
            $(this).css({background-image: url('<?php echo get_stylesheet_directory_uri(); ?>/img/person-hover.jpg')});    
        });      
    });
    var body = $( 'body' );
}); 

HTML: HTML:

<div class="silhoulettes">
    <a class="person" href="<?php echo get_permalink(41); ?>"></a>
</div>

There are some php wordpress functions between. 之间有一些php wordpress函数。

Thank you very much in advance! 提前非常感谢您!

The correct way of giving css via jQuery is css("propertyname","value"); 通过jQuery提供CSS的正确方法是css("propertyname","value"); and css({"propertyname":"value","propertyname":"value",...}); css({"propertyname":"value","propertyname":"value",...}); for multiple style. 用于多种样式。

Try this 尝试这个

$(".person").css({"background-image"," url('<?php echo get_stylesheet_directory_uri(); ?>'/img/person.jpg")});
$(".person").mouseover(function() {
     $(this).css({"background-image", "url('<?php echo get_stylesheet_directory_uri(); ?>'/img/person-hover.jpg")});    
 }); 

Changes Made 所做的更改

$(".person").css({background-image: url('<?php echo get_stylesheet_directory_uri(); ?>/img/person.jpg')}); 

to

$(".person").css({"background-image"," url('<?php echo get_stylesheet_directory_uri(); ?>'/img/person.jpg")});

Try below syntax 尝试以下语法

var imgUrl = '<?php echo get_stylesheet_directory_uri(); ?>/img/person.jpg';

$(".person").css("background-image","url("+ imgUrl +")");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM