Jquery javascript:html中的css样式调用不起作用

Jquery javascript:html中的css样式调用不起作用,jquery,html,Jquery,Html,嘿,我正在尝试学习jquery,当我使用jquery向div标记添加css类时,图像并没有显示,这是我的代码 HTML: <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Untitled 1</title> <script src="jquery-2.1.0

嘿,我正在尝试学习jquery,当我使用jquery向div标记添加css类时,图像并没有显示,这是我的代码

HTML:

<!DOCTYPE html>
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Untitled 1</title>
<script src="jquery-2.1.0.min.js"></script>
<script src="javascript_stuff.js"></script>
<link href="stylesheet.css" rel="stylesheet" type="text/css">

</head>
    <h1>trying to find out whats wrong with this code</h1>
    <h2>Here is the picture</h2>


<body>

<p></p>
<div class="thumbnails">
<a href="Images/User Images/early-sunrays.jpg">
<img alt="sunrays" height="75" src="Images/User%20Images/early-sunrays_thumbnail.jpg" width="75"></a>
</div>
<div class="preview"></div>

</body>
jquery代码:

$(document).ready(function(){
    $('.thumbnails a').click(function(e){
        e.preventDefault();
        var pic_full = $(this).attr('href');
        var size =(pic_full.length-4);
        var pic_prev = pic_full.slice(0,parseInt(size))+"_preview"+pic_full.slice(parseInt(size));
        pic_prev = "../"+pic_prev;
        //alert(pic_prev);
        $('.preview').html('<a href="'+pic_full+'" style="background-image:url('+pic_prev+');"></a>');
    });
});
$(文档).ready(函数(){
$('.thumbnails a')。单击(函数(e){
e、 预防默认值();
var pic_full=$(this.attr('href');
变量大小=(pic_全长-4);
var pic_prev=pic_full.slice(0,parseInt(size))+“_preview”+pic_full.slice(parseInt(size));
pic_prev=“..”+pic_prev;
//警报(pic_prev);
$('.preview').html('');
});
});

这可能是一个愚蠢的问题,但我似乎不能让它显示,即使路径是正确的,我试图在html中创建一个css样式..任何帮助将不胜感激。。谢谢

背景图像:url(../img/path)
中,图像路径应该用引号括起来:
背景图像:url('../img/path')

在您的情况下,将javascript的最后一行更改为:

$('.preview').html('')

(注意pic_preview周围的转义单引号)

此外,如果要使用
来显示背景图像,则可能需要将
的CSS更改为
display:block

.preview a {
  display: block;
  width: 75px;
  height: 75px;
  border:thick aqua solid;
}
JSFiddle:


希望这能让你开始。

你能分享小提琴吗?(jquery向div标签添加css类)我在你的问题中找不到,你能添加jsfiddle吗。
.preview a {
  display: block;
  width: 75px;
  height: 75px;
  border:thick aqua solid;
}