gpt4 book ai didi

javascript - 单击即可更改图像和报价?

转载 作者:行者123 更新时间:2023-12-03 04:04:34 25 4
gpt4 key购买 nike

我正在创建一个快速的小页面。我刚刚学习 JavaScript,需要一些时间来适应。我能够创建一个在单击图像时更改引号的页面。现在,我希望图像随引号而变化。因此,它将是:单击图像 1,将出现图像 2,并带有引用 2。单击图像 2,将出现图像 3,并带有引用 3。

这是一个非常简单的任务,但我一直无法找到一个简单的解决方案。我花了几个小时在这上面,这有点令人沮丧。

这是页面:https://aprilehrlich.github.io/dataint/

以下是我在 HTML 中得到的内容:

<!DOCTYPE html>
<html>
<head>
<title>Ida B. Wells</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<img id="MainProductImage"" src="https://i.ytimg.com/vi/pUMKFP4p2Lo/maxresdefault.jpg" width="500px"" id="myImg">
<h1>“Quote 1.”</h1>
<p>- Ida B. Wells</p>
</body>
</html>

JavaScript:

$(document).ready(function() {
var quote1 = "“Quote 1.”";
var quote2 = "“Quote 2”";
var quote3 = "“Quote 3”";
var quote4 = "“Quote 4”";
var quote5 = "“Quote 5”";
var quote6 = "“Quote 6”";
var quote7 = "“Quote 7”";
var quote8 = "“Quote 8”";
var quote9 = "“Quote 9”";
var quote10 = "“Quote 10”";
$("img").click(function() {
var currentQuote = $("h1").text();
$("h1").fadeOut(0);
if (currentQuote == quote1) {
$("h1").text(quote2);
}
if (currentQuote == quote2) {
$("h1").text(quote3);
}
if (currentQuote == quote3) {
$("h1").text(quote4);
}
if (currentQuote == quote4) {
$("h1").text(quote5);
}
if (currentQuote == quote5) {
$("h1").text(quote6);
}
if (currentQuote == quote6) {
$("h1").text(quote7);
}
if (currentQuote == quote7) {
$("h1").text(quote8);
}
if (currentQuote == quote8) {
$("h1").text(quote9);
}
if (currentQuote == quote9) {
$("h1").text(quote10);
}
$("h1").fadeIn(1000);
});
})

最佳答案

您必须使用attr(属性)函数来更改src 元素。这是一个例子。我使您的代码更加动态,因此您可以根据需要设置任意数量的引号,而无需更改代码:

HTML:

<img id="MainProductImage" src="https://s-media-cache-ak0.pinimg.com/564x/2a/b4/e7/2ab4e74ad637f9c8ecb792b8b7d605a6.jpg" id="myImg" data-current-quote="0">
<h1>“Quote 1.”</h1>
<p>- Ida B. Wells</p>

Javascript:

$(document).ready(function(){
//Quotes/Image Array
var quotes = [
{ quote: "Quote1", img: "https://s-media-cache-ak0.pinimg.com/564x/2a/b4/e7/2ab4e74ad637f9c8ecb792b8b7d605a6.jpg"},
{ quote: "Quote2", img: "https://s-media-cache-ak0.pinimg.com/564x/af/52/03/af5203d09a0be9c9e655786c88c1d8b7.jpg"},
{ quote: "Quote3", img: "https://s-media-cache-ak0.pinimg.com/564x/04/37/3c/04373c4f98797b202d13b9882e137690.jpg"}
];

$("img").click(function(){

var img = $("#MainProductImage"),
//We look for the next quote, if it's the last we go to the beginning
currentQuote = img.data("current-quote") === quotes.length -1 ? 0 : img.data("current-quote") + 1,
nextQuote = quotes[currentQuote];

//Set the current quote index in a data attribute
img.data("current-quote", currentQuote);
$("h1").fadeOut(0);
//Change the text
$("h1").text(nextQuote.quote);
//Change the image
img.attr("src", nextQuote.img);
});
});

fiddle :https://jsfiddle.net/p2bus29y/2/

关于javascript - 单击即可更改图像和报价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44616777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com