2013-10-17 84 views
0

我有一个PHP变量,我传递给一个jQuery脚本,我想设置某个div的背景。似乎没有任何工作。PHP + JQuery设置背景图像

下面的代码:

<?php $post_thumbnail_id = get_post_thumbnail_id(); ?> 
<?php $post_thumbnail_url = wp_get_attachment_url($post_thumbnail_id); ?> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#event-sub-hero").css('background', 'url("<?php echo $post_thumbnail_url ?>")'); 
    }); 
</script> 

<!-- SUBPAGE HEADER --> 
    <div id="event-sub-hero"> 
    <div class="container"> 
     <div class="sub-hero-text col-lg-6 col-lg-offset-3 col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3 col-xs-12"> 
     <h2><?php the_title(); ?></h2> 
     </div> <!-- end sub-hero-text --> 
    </div> <!-- end container --> 
    </div> <!-- end sub-hero --> 

<!-- END SUBPAGE HEADER --> 

这是它吐出来的是代码:

$(document).ready(function(){ 
     $("#event-sub-hero").css('background', 'url("http://localhost:8888/babble/wp-content/uploads/2013/10/madmen-night.jpg")'); 
    }); 
+1

$ post_thumbnail_url变量是否包含图像文件的完整路径名称,即images/pic1.gif – jeff

+1

生成的代码与预期生成的代码是什么? –

+0

什么具体不工作? –

回答

1

尝试background-image而不是CSS background

+0

试过了,没什么。 –

0

您是否尝试过只设置该元素的背景直线上升的CSS?也许这个元素并不像你期望的那样。另外,我也会使用背景图片。

0

不要使用双引号,你不应该需要它们,正如别人提到的那样,当你只设置背景图像时,使用'background-image'更加正确。

+0

删除了双引号。依然没有 。 –