Php jQuery没有';t显示背景图像

Php jQuery没有';t显示背景图像,php,jquery,Php,Jquery,我有个新手问题 我正在尝试显示a元素的背景图像。鼠标悬停时,图片应更改为不同的图片。 问题是没有显示图片。当我有CSS中的代码时,它工作得很好,所以一定是我的函数造成了问题 我的代码: jQuery(document).ready(function($){ $(document).ready(function() { $(".person").css({background-image: url('<?php echo get_stylesheet_director

我有个新手问题

我正在尝试显示a元素的背景图像。鼠标悬停时,图片应更改为不同的图片。 问题是没有显示图片。当我有CSS中的代码时,它工作得很好,所以一定是我的函数造成了问题

我的代码:

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' );
}); 
jQuery(文档).ready(函数($){
$(文档).ready(函数(){
$(“.person”).css({background image:url('/img/person.jpg')});
$(“.person”).mouseover(函数(){
$(this.css({background image:url('/img/person hover.jpg'))});
});      
});
变量body=$(“body”);
}); 
HTML:


之间有一些php wordpress函数

提前非常感谢

通过jQuery提供css的正确方法是
css(“propertyname”、“value”)
css({“propertyname”:“value”,“propertyname”:“value”,“…})用于多种样式

试试这个

$(".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")});    
 }); 
$(“.person”).css({“背景图像”,“url('''/img/person.jpg”)});
$(“.person”).mouseover(函数(){
$(this.css({“background image”,“url('''/img/person hover.jpg”)});
}); 
所做的更改

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

$(".person").css({"background-image"," url('<?php echo get_stylesheet_directory_uri(); ?>'/img/person.jpg")});
$(“.person”).css({“背景图像”,“url('''/img/person.jpg”)});
通过jQuery提供css的正确方法是
css(“propertyname”、“value”)
css({“propertyname”:“value”,“propertyname”:“value”,“…})用于多种样式

试试这个

$(".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")});    
 }); 
$(“.person”).css({“背景图像”,“url('''/img/person.jpg”)});
$(“.person”).mouseover(函数(){
$(this.css({“background image”,“url('''/img/person hover.jpg”)});
}); 
所做的更改

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

$(".person").css({"background-image"," url('<?php echo get_stylesheet_directory_uri(); ?>'/img/person.jpg")});
$(“.person”).css({“背景图像”,“url('''/img/person.jpg”)});
试试下面的语法

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

$(".person").css("background-image","url("+ imgUrl +")");
var-imgUrl='/img/person.jpg';
$(“.person”).css(“背景图像”、“url”(+imgUrl+));
试试下面的语法

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

$(".person").css("background-image","url("+ imgUrl +")");
var-imgUrl='/img/person.jpg';
$(“.person”).css(“背景图像”、“url”(+imgUrl+));


是否至少处理了PHP部分(需要在PHP文件中设置)?在浏览器中检查您获得的URL那么您是否检查了
get\u stylesheet\u directory\u URL()
的结果,并验证了结果URL的正确性?与问题无关,但为了更简洁,您可以在HTML中使用速记php标记
您需要在
css()
方法中设置的值周围加引号。检查控制台,因为您可能有语法错误是的,url是正确的。scottevans93-谢谢,非常方便。PHP部分是否至少已处理(需要在PHP文件中设置)?在浏览器中检查您获得的URL那么您是否检查了
get\u stylesheet\u directory\u URL()
的结果,并验证了结果URL的正确性?与问题无关,但为了更简洁,您可以在HTML中使用速记php标记
您需要在
css()
方法中设置的值周围加引号。检查控制台,因为您可能有语法错误是的,url是正确的。scottevans93-谢谢,非常方便。不幸的是,我遇到了一个错误:
Uncaught SyntaxError:Unexpected token,
我读到我可以像这样在jquery中使用css将元素组合在一起:
$('p').css({color:green,font size:14px})它工作!你用撇号犯了一些错误。非常感谢。请您对这部分发表意见:
$('p').css({颜色:绿色,字体大小:14px})。jQuery正确吗?您能告诉我在哪里可以更改撇号吗?因此我可以改进我的答案。在括号中的url之后应该是
,而不是
,在结束括号之后应该是
@Dandy你犯了一个错误
在添加CSSUN时是必需的。幸运的是,我遇到了一个错误:
未捕获的语法错误:意外的标记,
我读到我可以在jquery中像这样使用css将元素组合在一起:
$('p').css({color:green,font size:14px})
有效!您在撇号上犯了一些错误。谢谢。请您对此部分发表评论:
$('p').css({颜色:绿色,字体大小:14px});
。jQuery正确吗?你能告诉我在哪里可以更改撇号吗?这样我就可以改进我的答案。括号中的url后面应该是
,而不是
,在结束括号之后应该是
@Dandy你犯了一个错误。
在添加css时是必要的