【发布时间】:2020-11-18 03:41:56
【问题描述】:
W3Schools 有一个 example 我用来制作图片标签库。
他们使用纯 JS 的示例,我正在尝试使用 jQuery。我是使用 jQuery 的新手,但我有一个功能似乎可以在控制台中抓取图像,但所选图像不会出现在 ID 为 #expandedImg 的 img 元素中。
我似乎无法弄清楚什么是错误的,或者为什么它不会呈现我单击的图像。我将不胜感激任何指导。谢谢。
$(document).ready(function() {
$(".column img").click(function() {
var newclass = $(this).attr("src");
console.log(newclass);
var oldclass = $("#expandedImg").attr("id");
console.log(oldclass);
$("#expandedImg").fadeOut(function() {
$("#expandedImg").removeClass(oldclass).addClass(newclass).fadeIn("slow");
console.log(newclass);
});
});
});
/* The grid: Four equal columns that floats next to each other */
.column {
float: left;
width: 20%;
padding: 10px;
}
/* Style the images inside the grid */
.column img {
opacity: 0.8;
cursor: pointer;
}
.column img:hover {
opacity: 1;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* The expanding image container */
.container {
position: relative;
display: none;
width: 50%;
}
/* Closable button inside the expanded image */
.closebtn {
position: absolute;
top: 10px;
right: 15px;
color: white;
font-size: 35px;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Gallery</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
<link href="../jquery-ui/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="../jquery-ui/external/jquery/jquery.js" type="text/javascript"></script>
<script src="../jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
</head>
<body>
<!-- The four columns -->
<div class="row">
<div class="column">
<img src="../images/trees_mountains.jpg" alt="Nature" style="width:100%">
</div>
<div class="column">
<img src="../images/snow_mountains.jpg" alt="Snow" style="width:100%">
</div>
<div class="column">
<img src="../images/mountain_landscape.jpg" alt="Mountains" style="width:100%">
</div>
<div class="column">
<img src="../images/skylights.jpg" alt="Lights" style="width:100%">
</div>
</div>
<div class="container">
<span onclick="this.parentElement.style.display='none'" class="closebtn">×</span>
<img id="expandedImg" style="width:100%" />
</div>
</body>
</html>
【问题讨论】:
-
控制台有错误吗? HTML 中第一个代码块中的
<script>在哪里?好像没有被引用 -
控制台中没有错误。该脚本位于 html 中 body 标记的底部。
标签: javascript html jquery css