gpt4 book ai didi

javascript - 将图像移动到一组图像的右侧或左侧 onClick

转载 作者:行者123 更新时间:2023-11-30 19:16:31 25 4
gpt4 key购买 nike

所以我试图这样做,以便在点击页面底部的 Frog 时,它每次都会转到旁边图像的另一侧。到目前为止,我已经能够做到一次,但我不知道如何通过这两种方法来做更多。任何帮助将不胜感激。 (如果您想知道为什么缺少某些内容,我删除了一些个人信息。)

<html>

<head>
<title>
Assignment 4
</title>
<script>
function AlertStudent() {
alert("");
}

function ConsoleStudent() {
console.log("");
}

function ShowStudent() {
var x = document.getElementById("student");
x.style.display = "block";
}

function HideStudent() {
var x = document.getElementById("student");
x.style.display = "none";
}

function AlertSubject() {
alert("");
}

function ConsoleSubject() {
console.log("");
}

function ShowSubject() {
var x = document.getElementById("subject");
x.style.display = "block";
}

function HideSubject() {
var x = document.getElementById("subject");
x.style.display = "none";
}

function AlertAssignment() {
alert("Assignment 4 - 14th September 2019");
}

function ConsoleAssignment() {
console.log("Assignment 4 - 14th September 2019");
}

function ShowAssignment() {
var x = document.getElementById("assignment");
x.style.display = "block";
}

function HideAssignment() {
var x = document.getElementById("assignment");
x.style.display = "none";
}

function AlertLab() {
alert("Wednesday 8:30-10:30");
}

function ConsoleLab() {
console.log("Wednesday 8:30-10:30");
}

function ShowLab() {
var x = document.getElementById("lab");
x.style.display = "block";
}

function HideLab() {
var x = document.getElementById("lab");
x.style.display = "none";
}

function SwitchParagraphs() {
var p1Text = p1.textContent;
p1.textContent = p2.textContent;
p2.textContent = p1Text;
}

function Cat() {
document.getElementById("myText").value = "User clicks Cat";

var displaySpan = document.getElementById("display");
displaySpan.innerHTML = "Cat is clicked";

var image = document.getElementById("displayImg");
image.src = "cat.png";
}

function Dog() {
document.getElementById("myText").value = "User clicks Dog";

var displaySpan = document.getElementById("display");
displaySpan.innerHTML = "Dog is clicked";

var image = document.getElementById("displayImg");
image.src = "dog.png";
}

function Frog() {
document.getElementById("myText").value = "User clicks Frog";

var displaySpan = document.getElementById("display");
displaySpan.innerHTML = "Frog is clicked";

var image = document.getElementById("displayImg");
image.src = "frog.png";
}

var counter = 0;
var counter1 = 0;

function Hour() {
counter = counter + 1;
var counterSpan = document.getElementById("counterHour");
counterSpan.innerHTML = counter;
if (counter >= 23) {
counter = -1;
}
}

function Minute() {
counter1 = counter1 + 1;
var counterSpan = document.getElementById("counterMinute");
counterSpan.innerHTML = counter1;
if (counter1 >= 59) {
counter1 = -1;
}
}

function AlertAnimal() {
alert("Don’t click me, click the frog");
}

function FrogChange() {
var image = document.getElementById("displayImg1");
image.src = "frog.png";

var x = document.getElementById("frog");
x.style.display = "none";
}

function FrogClick2() {
var image = document.getElementById("displayImg3");
image.src = "frog.png";
var x = document.getElementById("displayImg1");
x.style.display = "none";
}
</script>
</head>

<body>
<div id="student"></div>
<button onClick="ShowStudent()">Show</button>
<button onClick="HideStudent()">Hide</button>
<button onClick="AlertStudent()">Alert</button>
<button onClick="ConsoleStudent()">Console</button>
<br>
<br>
<div id="subject"></div>
<button onClick="ShowSubject()">Show</button>
<button onClick="HideSubject()">Hide</button>
<button onClick="AlertSubject()">Alert</button>
<button onClick="ConsoleSubject()">Console</button>
<br>
<br>
<div id="assignment">Assignment 4 - 14th September 2019</div>
<button onClick="ShowAssignment()">Show</button>
<button onClick="HideAssignment()">Hide</button>
<button onClick="AlertAssignment()">Alert</button>
<button onClick="ConsoleAssignment()">Console</button>
<br>
<br>
<div id="lab">Wednesday 8:30-10:30</div>
<button onClick="ShowLab()">Show</button>
<button onClick="HideLab()">Hide</button>
<button onClick="AlertLab()">Alert</button>
<button onClick="ConsoleLab()">Console</button>
<br>
<br>
<hr />
<p id="p1"></p>
<button onClick="SwitchParagraphs()">Switch Paragraphs</button>
<p id="p2"></p>
<hr />
<button onClick="Cat()">Cat</button>
<button onClick="Dog()">Dog</button>
<button onClick="Frog()">Frog</button>
<br>
<br>
<input type="text" id="myText" />
<br>
<br>
<span id="display"></span>
<br>
<br>
<img id="displayImg" />
<hr />
<button onClick="Hour()">Hour</button>
<font size="7">
<span id="counterHour">0</span>:<span id="counterMinute">0</span></font>
<button onClick="Minute()">Minute</button>
<hr />
<img id="displayImg1" onClick="FrogClick2()" /><img id="displayImg2" />
<img src="cat.png" ; onClick="AlertAnimal()" ; id="cat" /><img
src="dog.png" ; id="dog" ; onClick="AlertAnimal()" ;/>
<img src="frog.png" ; onClick="FrogChange()" ; id="frog" /><img id="displayImg3" onClick="FrogFlick3()" />

最佳答案

与其使用不同的 javascript 函数来移动 Frog ,不如只使用一个。通过在函数参数中使用“this”来提供 Frog 所在的当前元素。

        <script>
function AlertStudent() {
alert("");
}

function ConsoleStudent() {
console.log("");
}

function ShowStudent() {
var x = document.getElementById("student");
x.style.display = "block";
}

function HideStudent() {
var x = document.getElementById("student");
x.style.display = "none";
}

function AlertSubject() {
alert("");
}

function ConsoleSubject() {
console.log("");
}

function ShowSubject() {
var x = document.getElementById("subject");
x.style.display = "block";
}

function HideSubject() {
var x = document.getElementById("subject");
x.style.display = "none";
}

function AlertAssignment() {
alert("Assignment 4 - 14th September 2019");
}

function ConsoleAssignment() {
console.log("Assignment 4 - 14th September 2019");
}

function ShowAssignment() {
var x = document.getElementById("assignment");
x.style.display = "block";
}

function HideAssignment() {
var x = document.getElementById("assignment");
x.style.display = "none";
}

function AlertLab() {
alert("Wednesday 8:30-10:30");
}

function ConsoleLab() {
console.log("Wednesday 8:30-10:30");
}

function ShowLab() {
var x = document.getElementById("lab");
x.style.display = "block";
}

function HideLab() {
var x = document.getElementById("lab");
x.style.display = "none";
}

function SwitchParagraphs() {
var p1Text = p1.textContent;
p1.textContent = p2.textContent;
p2.textContent = p1Text;
}

function Cat() {
document.getElementById("myText").value = "User clicks Cat";

var displaySpan = document.getElementById("display");
displaySpan.innerHTML = "Cat is clicked";

var image = document.getElementById("displayImg");
image.src = "cat.png";
}

function Dog() {
document.getElementById("myText").value = "User clicks Dog";

var displaySpan = document.getElementById("display");
displaySpan.innerHTML = "Dog is clicked";

var image = document.getElementById("displayImg");
image.src = "dog.png";
}

function Frog() {
document.getElementById("myText").value = "User clicks Frog";

var displaySpan = document.getElementById("display");
displaySpan.innerHTML = "Frog is clicked";

var image = document.getElementById("displayImg");
image.src = "frog.png";
}

var counter = 0;
var counter1 = 0;

function Hour() {
counter = counter + 1;
var counterSpan = document.getElementById("counterHour");
counterSpan.innerHTML = counter;
if (counter >= 23) {
counter = -1;
}
}

function Minute() {
counter1 = counter1 + 1;
var counterSpan = document.getElementById("counterMinute");
counterSpan.innerHTML = counter1;
if (counter1 >= 59) {
counter1 = -1;
}
}

function AlertAnimal() {
alert("Don’t click me, click the frog");
}

function FrogChange(el) {
console.log(el.id);
if(el.id == "displayImg3" ){
document.getElementById("displayImg3").src = "";
document.getElementById("displayImg1").src = "frog.png";
}
else {
document.getElementById("displayImg3").src = "frog.png";
document.getElementById("displayImg1").src = "";
}

// var image = document.getElementById("displayImg1");
// image.src = "frog.png";
//
// var x = document.getElementById("frog");
// x.style.display = "none";
}

function FrogClick2() {
var image = document.getElementById("displayImg3");
image.src = "frog.png";
var x = document.getElementById("displayImg1");
x.style.display = "none";
}
</script>

<div id="student"></div>
<button onClick="ShowStudent()">Show</button>
<button onClick="HideStudent()">Hide</button>
<button onClick="AlertStudent()">Alert</button>
<button onClick="ConsoleStudent()">Console</button>
<br>
<br>
<div id="subject"></div>
<button onClick="ShowSubject()">Show</button>
<button onClick="HideSubject()">Hide</button>
<button onClick="AlertSubject()">Alert</button>
<button onClick="ConsoleSubject()">Console</button>
<br>
<br>
<div id="assignment">Assignment 4 - 14th September 2019</div>
<button onClick="ShowAssignment()">Show</button>
<button onClick="HideAssignment()">Hide</button>
<button onClick="AlertAssignment()">Alert</button>
<button onClick="ConsoleAssignment()">Console</button>
<br>
<br>
<div id="lab">Wednesday 8:30-10:30</div>
<button onClick="ShowLab()">Show</button>
<button onClick="HideLab()">Hide</button>
<button onClick="AlertLab()">Alert</button>
<button onClick="ConsoleLab()">Console</button>
<br>
<br>
<hr />
<p id="p1"></p>
<button onClick="SwitchParagraphs()">Switch Paragraphs</button>
<p id="p2"></p>
<hr />
<button onClick="Cat()">Cat</button>
<button onClick="Dog()">Dog</button>
<button onClick="Frog()">Frog</button>
<br>
<br>
<input type="text" id="myText" />
<br>
<br>
<span id="display"></span>
<br>
<br>
<img id="displayImg" />
<hr />
<button onClick="Hour()">Hour</button>
<font size="7">
<span id="counterHour">0</span>:<span id="counterMinute">0</span></font>
<button onClick="Minute()">Minute</button>
<hr />
<img id="displayImg1" onClick="FrogChange(this)" />
<img src="cat.png" onClick="AlertAnimal();" id="cat" />
<img src="dog.png" id="dog" onClick="AlertAnimal();" />
<img id="displayImg3" src="frog.png" onClick="FrogChange(this);" />

关于javascript - 将图像移动到一组图像的右侧或左侧 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932918/

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