英文:
Changing image in Javascript isn't showing even when setting the source correctly
问题
HTML:
我将一个div(imagen)放在另一个div(box)中,并希望在按下其中一个按钮时更改图像的src,并显示相应的图像,但它只显示alt,就像找不到图像一样。如果某些代码与问题无关,我很抱歉,只是想将它们全部放在一起,以便有人想尝试自己编写代码。
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Change DIV Colors</title>
<link rel="stylesheet" href="./styles/styles.css">
</head>
<body>
<header>
<div class="text"><p id="title">With getElementBy and querySelector, we can turn HTML elements into JavaScript variables and edit various attributes.</p></div>
</header>
<div class="clock">
<h1 class="time"></h1>
<h2 class="dayOfWeek"></h2>
<h2 class="date"></h2>
</div>
<div class="dimensions">
<h3 class="height"></h3>
<h3>x</h3>
<h3 class="width"></h3>
</div>
<div class="content">
<section class="buttons">
<input type="button" value="red" id="btnRed" onclick="setColorRed()">
<input type="button" value="green" id="btnGreen" onclick="setColorGreen()">
<input type="button" value="blue" id="btnBlue" onclick="setColorBlue()">
<input type="button" value="yellow" id="btnYellow" onclick="setColorYellow()">
<input type="button" value="purple" id="btnPurple" onclick="setColorPurple()">
<input type="button" value="reset" id="btnReset" onclick="setColorReset()">
</section>
<div class="box">
<img class="image" src="./styles/imgs/1.jpg" alt="Changing Image">
</div>
</div>
<script src="js/changecolor.js"></script>
</body>
</html>
CSS:
* {
margin: 0;
padding: 0;
}
body {
background: rgb(32, 61, 37);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
header {
background-color: rgb(140, 181, 142);
text-align: center;
}
.text {
font-size: 20px;
}
.content {
margin-top: -65px;
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.buttons {
display: flex;
flex-direction: column;
}
input {
width: 80px;
height: 30px;
background-color: rgb(191, 219, 29);
cursor: pointer;
outline: none;
font-weight: bold;
text-transform: capitalize;
border-radius: 4px;
margin: 10px;
}
input:hover {
background-color: red;
color: white;
}
input:active {
background-color: rgb(5, 208, 5);
}
.box {
width: 550px;
height: 350px;
border: 3px groove white;
background: #000;
display: flex;
justify-content: center;
align-items: center;
}
.image {
height: 60%;
width: 60%;
}
.clock {
margin: 15px;
text-align: center;
}
.dimensions {
height: 65px;
width: 150px;
border: 1px dashed;
display: flex;
flex-direction: column;
text-align: center;
}
JavaScript:
let btnRed = document.getElementById('btnRed');
let btnGreen = document.getElementById('btnGreen');
let btnBlue = document.getElementById('btnBlue');
let btnYellow = document.getElementById('btnYellow');
let btnPurple = document.getElementById('btnPurple');
let btnReset = document.getElementById('btnReset');
let box = document.querySelector('.box');
let image = document.querySelector(".image");
let title = document.querySelector("#title");
function setColorRed() {
if(btnRed.value == "red") {
box.style.background = "#FF0000";
image.src = "./imgs/2.jpg";
title.style.color = "red";
}
}
function setColorGreen() {
if(btnGreen.value == "green") {
box.style.background = "#00FF00";
image.src = "./imgs/3.jpg";
title.style.color = "green";
}
}
function setColorBlue() {
if(btnBlue.value == "blue") {
box.style.background = "#0000FF";
image.src = "./imgs/4.jpg";
title.style.color = "blue";
}
}
function setColorYellow() {
if(btnYellow.value == "yellow") {
box.style.background = "#FFFF00";
image.src = "./imgs/5.png";
title.style.color = "yellow";
}
}
function setColorPurple() {
if(btnPurple.value == "purple") {
box.style.background = "#842593";
image.src = "./imgs/1.jpg";
title.style.color = "#395248";
}
}
function setColorReset() {
if(btnReset.value == "reset") {
box.style.background = "#000000";
image.src = "./imgs/1.jpg";
title.style.color = "black";
}
}
let time = document.querySelector(".time");
setInterval(() => {
let date = new Date();
time.innerText = date.toLocaleTimeString();
}, 1000);
let dayOfWeek = document.querySelector(".dayOfWeek");
let date = new Date();
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
dayOfWeek.innerText = days[date.getDay()];
let dateDisplay = document.querySelector(".date");
let currentDate = new Date();
dateDisplay.innerText = currentDate.toLocaleDateString();
function ask() {
setInterval(() => {
if (!confirm("Do you want to stay on this page?")) {
window.location.href = "https://www.onlinevideoconverter.vip";
}
}, 180000);
}
ask();
let height = document.querySelector(".height");
let width = document.querySelector(".width");
setInterval(() => {
height.innerText = innerHeight;
width.innerText = innerWidth;
}, 1);
英文:
I put a div(imagen) inside of the div(box) and want the src of the image to change when pushing one of the buttons and show the corresponding image, but it's only showing the alt as if the image could not be found. Sorry if some of the code isn't related with the question but just wanted to put it all in case someone wanted to try it themselves.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Cambiar colores de un DIV</title>
<link rel="stylesheet" href="./styles/styles.css">
</head>
<body>
<header>
<div class="texto"><p id="titulo">Con getElementBy y querySelector podemos transformar elementos del HTML a variables en javascript y editar varios de sus atributos.</p></div>
</header>
<div class="reloj">
<h1 class="hora"></h1>
<h2 class="diaSemana"></h2>
<h2 class="fecha"></h2>
</div>
<div class="medidas">
<h3 class="alto"></h3>
<h3>x</h3>
<h3 class="ancho"></h3>
</div>
<div class="contenido">
<section class="botones">
<input type="button" value="rojo" id="btnrojo" onclick="setColorRojo()">
<input type="button" value="verde" id="btnverde" onclick="setColorVerde()">
<input type="button" value="azul" id="btnazul" onclick="setColorAzul()">
<input type="button" value="amarillo" id="btnamarillo" onclick="setColorAmarillo()">
<input type="button" value="morado" id="btnmorado" onclick="setColorMorado()">
<input type="button" value="reset" id="btnreset" onclick="setColorReset()">
</section>
<div class="box">
<img class="imagen" src="./styles/imgs/1.jpg" alt="Imágen cambiante">
</div>
</div>
<script src="js/changecolor.js"></script>
</body>
</html>
CSS:
* {
margin: 0;
padding: 0;
}
body {
background: rgb(32, 61, 37);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
header {
background-color: rgb(140, 181, 142);
text-align: center;
}
.texto {
font-size: 20px;
}
.contenido {
margin-top: -65px;
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.botones{
display: flex;
flex-direction: column;
}
input {
width: 80px;
height: 30px;
background-color: rgb(191, 219, 29);
cursor: pointer;
outline: none;
font-weight: bold;
text-transform: capitalize;
border-radius: 4px;
margin: 10px;
}
input:hover {
background-color: red;
color: white;
}
input:active {
background-color: rgb(5, 208, 5);
}
.box {
width: 550px;
height: 350px;
border: 3px groove white;
background: #000;
display: flex;
justify-content: center;
align-items: center;
}
.imagen {
height: 60%;
width: 60%;
}
.reloj {
margin: 15px;
text-align: center;
}
.medidas {
height: 65px;
width: 150px;
border: 1px dashed;
display: flex;
flex-direction: column;
text-align: center;
}
Javascript:
let btnRojo = document.getElementById('btnrojo');
let btnVerde = document.getElementById('btnverde');
let btnAzul = document.getElementById('btnazul');
let btnAmarillo= document.getElementById('btnamarillo');
let btnMorado= document.getElementById('btnmorado');
let btnReset = document.getElementById('btnreset');
let box = document.querySelector('.box');
let imagen = document.querySelector(".imagen");
let titulo = document.querySelector("#titulo");
function setColorRojo() {
if(btnRojo.value == "rojo") {
box.style.background = "#FF0000";
imagen.src = "./imgs/2.jpg";
titulo.style.color = "red";
}
}
function setColorVerde() {
if(btnVerde.value == "verde") {
box.style.background = "#00FF00";
imagen.src = "./imgs/3.jpg";
titulo.style.color = "green";
}
}
function setColorAzul() {
if(btnAzul.value == "azul") {
box.style.background = "#0000FF";
imagen.src = "./imgs/4.jpg";
titulo.style.color = "blue";
}
}
function setColorAmarillo() {
if(btnAmarillo.value == "amarillo") {
box.style.background = "#FFFF00";
imagen.src = "./imgs/5.png";
titulo.style.color = "yellow";
}
}
function setColorMorado() {
if(btnMorado.value == "morado") {
box.style.background = "#842593";
imagen.src = "./imgs/1.jpg";
titulo.style.color = "#395248";
}
}
function setColorReset() {
if(btnReset.value == "reset") {
box.style.background = "#000000";
imagen.src = "./imgs/1.jpg";
titulo.style.color = "black";
}
}
let hora = document.querySelector(".hora");
setInterval(() => {
let date = new Date();
hora.innerText = date.toLocaleTimeString();
}, 1000);
let diaSemana = document.querySelector(".diaSemana");
let dia = new Date();
const d = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
diaSemana.innerText = d[dia.getDay()];
let fecha = document.querySelector(".fecha");
let date = new Date();
fecha.innerText = date.toLocaleDateString();
function pregunta() {
setInterval(() => {
if (!confirm("¿Quieres seguir en esta página?")) {
window.location.replace = "https://www.onlinevideoconverter.vip";
}
}, 180000);
}
pregunta();
let alto = document.querySelector(".alto");
let ancho = document.querySelector(".ancho");
setInterval(() => {
alto.innerText = innerHeight;
ancho.innerText = innerWidth;
}, 1);
I tried using setAttribute, same resutls. Even tried using a div and setting the images as backgrounds but that had less results, but if you use this method and it gives good results it's also welcomed as an answer thx.
答案1
得分: 0
您的问题仅在于未在JavaScript中包含样式。
以下是已修复的JavaScript代码:
let btnRojo = document.getElementById('btnrojo');
let btnVerde = document.getElementById('btnverde');
let btnAzul = document.getElementById('btnazul');
let btnAmarillo = document.getElementById('btnamarillo');
let btnMorado = document.getElementById('btnmorado');
let btnReset = document.getElementById('btnreset');
let box = document.querySelector('.box');
let imagen = document.querySelector(".imagen");
let titulo = document.querySelector("#titulo");
function setColorRojo() {
if (btnRojo.value == "rojo") {
box.style.background = "#FF0000";
imagen.src = "./styles/imgs/2.jpg";
titulo.style.color = "red";
}
}
function setColorVerde() {
if (btnVerde.value == "verde") {
box.style.background = "#00FF00";
imagen.src = "./styles/imgs/3.jpg";
titulo.style.color = "green";
}
}
function setColorAzul() {
if (btnAzul.value == "azul") {
box.style.background = "#0000FF";
imagen.src = "./styles/imgs/4.jpg";
titulo.style.color = "blue";
}
}
function setColorAmarillo() {
if (btnAmarillo.value == "amarillo") {
box.style.background = "#FFFF00";
imagen.src = "./styles/imgs/5.png";
titulo.style.color = "yellow";
}
}
function setColorMorado() {
if (btnMorado.value == "morado") {
box.style.background = "#842593";
imagen.src = "./styles/imgs/1.jpg";
titulo.style.color = "#395248";
}
}
function setColorReset() {
if (btnReset.value == "reset") {
box.style.background = "#000000";
imagen.src = "./styles/imgs/1.jpg";
titulo.style.color = "black";
}
}
let hora = document.querySelector(".hora");
setInterval(() => {
let date = new Date();
hora.innerText = date.toLocaleTimeString();
}, 1000);
let diaSemana = document.querySelector(".diaSemana");
let dia = new Date();
const d = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
diaSemana.innerText = d[dia.getDay()];
let fecha = document.querySelector(".fecha");
let date = new Date();
fecha.innerText = date.toLocaleDateString();
function pregunta() {
setInterval(() => {
if (!confirm("¿Quieres seguir en esta página?")) {
window.location.replace("https://www.onlinevideoconverter.vip");
}
}, 180000);
}
pregunta();
let alto = document.querySelector(".alto");
let ancho = document.querySelector(".ancho");
setInterval(() => {
alto.innerText = innerHeight;
ancho.innerText = innerWidth;
}, 1);
这是一个显示修复的Replit链接:https://replit.com/@JackBaldyga/soffix?v=1
英文:
your issue is simply not including styles in your JS
Here is the fixed JS
let btnRojo = document.getElementById('btnrojo');
let btnVerde = document.getElementById('btnverde');
let btnAzul = document.getElementById('btnazul');
let btnAmarillo= document.getElementById('btnamarillo');
let btnMorado= document.getElementById('btnmorado');
let btnReset = document.getElementById('btnreset');
let box = document.querySelector('.box');
let imagen = document.querySelector(".imagen");
let titulo = document.querySelector("#titulo");
function setColorRojo() {
if(btnRojo.value == "rojo") {
box.style.background = "#FF0000";
imagen.src = "./styles/imgs/2.jpg";
titulo.style.color = "red";
}
}
function setColorVerde() {
if(btnVerde.value == "verde") {
box.style.background = "#00FF00";
imagen.src = "./styles/imgs/3.jpg";
titulo.style.color = "green";
}
}
function setColorAzul() {
if(btnAzul.value == "azul") {
box.style.background = "#0000FF";
imagen.src = "./styles/imgs/4.jpg";
titulo.style.color = "blue";
}
}
function setColorAmarillo() {
if(btnAmarillo.value == "amarillo") {
box.style.background = "#FFFF00";
imagen.src = "./styles/imgs/5.png";
titulo.style.color = "yellow";
}
}
function setColorMorado() {
if(btnMorado.value == "morado") {
box.style.background = "#842593";
imagen.src = "./styles/imgs/1.jpg";
titulo.style.color = "#395248";
}
}
function setColorReset() {
if(btnReset.value == "reset") {
box.style.background = "#000000";
imagen.src = "./styles/imgs/1.jpg";
titulo.style.color = "black";
}
}
let hora = document.querySelector(".hora");
setInterval(() => {
let date = new Date();
hora.innerText = date.toLocaleTimeString();
}, 1000);
let diaSemana = document.querySelector(".diaSemana");
let dia = new Date();
const d = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
diaSemana.innerText = d[dia.getDay()];
let fecha = document.querySelector(".fecha");
let date = new Date();
fecha.innerText = date.toLocaleDateString();
function pregunta() {
setInterval(() => {
if (!confirm("¿Quieres seguir en esta página?")) {
window.location.replace = "https://www.onlinevideoconverter.vip";
}
}, 180000);
}
pregunta();
let alto = document.querySelector(".alto");
let ancho = document.querySelector(".ancho");
setInterval(() => {
alto.innerText = innerHeight;
ancho.innerText = innerWidth;
}, 1);
here is a replit showing the fix: https://replit.com/@JackBaldyga/soffix?v=1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论