通过jQuery更改CSS背景图像不起作用

时间:2017-01-04 18:20:39

标签: jquery html css

我正在使用以下代码,似乎无法弄清楚它为什么不起作用。我的目标是根据右侧6个图像之一的点击更改.featured-artist类的背景图像。

完整HTML

<!DOCTYPE html>
<html>
<head>
<title>PP - TEST</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<!-- Bootstrap core CSS -->
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" media="screen">

<!-- Load FontAwesome Icons -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

<!-- Posted Prints CSS -->
<link href="../html/css/posted-prints.css" rel="stylesheet" media="screen">

<!-- Load Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">

<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
      <script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
      <script src="http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
    <![endif]-->
</head>
<body>
<div id="artist" class="container-fluid featured-artist">
  <div class="col-lg-3 artist-bio pull-right">
    <h4>JOHN DOE</h4>
    <h6><em>Artist Bio</em></h6>
    <hr>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <p class="artist-link text-right"><em><a href="#">View all work from this artist</a></em></p>
    <div class="row">
      <div class="col-lg-4 artist-photo"> <img src="http://michaelfroseth.com/clients/postedprints/html/img/ER 1 (original).png" class="img-responsive" /> </div>
      <div class="col-lg-4 artist-photo"> <img src="http://michaelfroseth.com/clients/postedprints/html/img/ER 1 (original).png" class="img-responsive" /> </div>
      <div class="col-lg-4 artist-photo"> <img src="http://michaelfroseth.com/clients/postedprints/html/img/CG 7 (original).png" class="img-responsive" /> </div>
    </div>
    <div class="row">
      <div class="col-lg-4 artist-photo"> <img src="http://michaelfroseth.com/clients/postedprints/html/img/ER 4 (original).png" class="img-responsive" /> </div>
      <div class="col-lg-4 artist-photo"> <img src="http://michaelfroseth.com/clients/postedprints/html/img/ER 2 (original).png" class="img-responsive" /> </div>
      <div class="col-lg-4 artist-photo"> <img src="http://michaelfroseth.com/clients/postedprints/html/img/other artists_F.png" class="img-responsive" /> </div>
    </div>
  </div>
</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
<!-- Include all compiled plugins (below), or include individual files as needed --> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script> 
<script>
    $('.artist-photo img').click(function(){
  var images = $('.artist-photo img').attr('src');
    $('.featured-artist').css('background-image','url('+images+')');
});
</script> 
<script>
    $('a').click(function(){
    $('html, body').animate({
        scrollTop: $( $(this).attr('href') ).offset().top
    }, 500);
    return false;
});
</script>
</body>
</html>

JQUERY

<script>
    $('.artist-photo img').click(function(){
  var images = $('.artist-photo img').attr('src');
    $('.featured-artist').css('background-image','url('+images+')');
});
</script> 

CSS

.featured-artist {
    background-image: url('http://michaelfroseth.com/clients/postedprints/html/img/ER 8 (original) copy.png');
    margin-left: 0px;
    margin-right: 0px;
    padding-left: 0px;
    padding-right: 0px;
}

有没有人有一个工作的jsfiddle使用类似的功能,我可以学习?

1 个答案:

答案 0 :(得分:4)

我发现你的脚本存在两个问题;

  • 您正在为图片变量分配多个值
  • 您的背景图片网址缺少引号

试试这个

<script>
$(".artist-photo img").click(function(){
var images = $(this).attr('src');
$(".featured-artist").css("background-image","url('"+images+"')");
});
</script>