gpt4 book ai didi

Javascript 图像 onclick

转载 作者:行者123 更新时间:2023-11-30 15:52:01 25 4
gpt4 key购买 nike

设计一个页面,其中包含许多可供最终用户选择的图像。尝试使用事件(onclick、onmouseover 和 onmouseout)使其在图形上令人愉悦。由于每个事件都会更改多个 div 标签,因此我决定使用 Javascript 而不是 CSS。

为了防止 onmouseout 影响点击的图像边框,我分配了一个变量“imgClicked”。问题是点击的每张图片都会更改相同的变量。有没有办法为每个框分配变量,以便每个图像都知道它是否被单击。或者也许有更好的路线?

提前致谢!

  var borderColorOver = "#26d314";
var borderColorOff = "#7DAFE7";
var borderColorOn = "#d40101";
var imgClicked = 0;

function heartMouseClick(a) {
var heartImg = a;
if (imgClicked < 1) {
var images = document.getElementsByClassName('images');
images[heartImg].style.borderColor = borderColorOn;
var subject = document.getElementsByClassName('subject');
subject[heartImg].style.borderColor = borderColorOn;
imgClicked = 1;
} else {
var images = document.getElementsByClassName('images');
images[heartImg].style.borderColor = borderColorOff;
var subject = document.getElementsByClassName('subject');
subject[heartImg].style.borderColor = borderColorOff;
imgClicked = 0;
}
}

function heartMouseOver(a) {
var heartImg = a;
if (imgClicked < 1) {
var images = document.getElementsByClassName('images');
images[heartImg].style.borderColor = borderColorOver;
var subject = document.getElementsByClassName('subject');
subject[heartImg].style.borderColor = borderColorOver;
}
}

function heartMouseOut(a) {
var heartImg = a;
if (imgClicked < 1) {
var images = document.getElementsByClassName('images');
images[heartImg].style.borderColor = borderColorOff;
var subject = document.getElementsByClassName('subject');
subject[heartImg].style.borderColor = borderColorOff;
}
}
.pictureBox {
width: 300px;
height: 300px;
border-style: solid;
border-width: 0px;
}
.images {
width: 300px;
height: 200px;
}
.subject {
width: 300px;
height: 100px;
border: 5px solid #FFBE00;
}
<body>

<div class="pictureBox">
<div class="picture">
<img onmouseover="heartMouseOver(0)" onmouseout="heartMouseOut(0)" onclick="heartMouseClick(0)" class="images" border="5" src="photo-1.jpg">
</div>
<div class="subject" border="5">
Image 0 Description
</div>
</div>
<div class="pictureBox">
<div>
<img onmouseover="heartMouseOver(1)" onmouseout="heartMouseOut(1)" onclick="heartMouseClick(1)" class="images" border="5" src="photo-2.jpg">
</div>
<div class="subject" border="5">
Image 1 Description
</div>
</div>
<div class="pictureBox">
<div>
<img onmouseover="heartMouseOver(2)" onmouseout="heartMouseOut(2)" onclick="heartMouseClick(2)" class="images" border="5" src="photo-3.jpg">
</div>
<div class="subject" border="5">
Image 2 Description
</div>
</div>

</body>

最佳答案

我已经对您的代码进行了一些修改。我将 id 添加到 dom 元素并将 id 传递给所有方法,而不是对它们进行硬编码。这是完整的新代码:

        <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

<style type="text/css">
.pictureBox {
width: 300px;
height: 300px;
border-style: solid;
border-width: 0px;
}
.images {
width: 300px;
height: 200px;
}
.subject {
width: 300px;
height: 100px;
border: 5px solid #FFBE00;
}
</style>

</head>

<script>
var borderColorOver = "#26d314";
var borderColorOff = "#7DAFE7";
var borderColorOn = "#d40101";
var imgClicked = 0;

function heartMouseClick(a) {

if (imgClicked < 1) {
var images = document.getElementById(a);
images.style.borderColor = borderColorOn;
var subject = document.getElementById('s'+a);
subject.style.borderColor = borderColorOn;
imgClicked = 1;
} else {
var images = document.getElementById(a);
images.style.borderColor = borderColorOff;
var subject = document.getElementById('s'+a);
subject.style.borderColor = borderColorOff;
imgClicked = 0;
}
}
function heartMouseOver(a) {

if (imgClicked < 1) {
var images = document.getElementById(a);
images.style.borderColor = borderColorOver;
var subject = document.getElementById('s'+a);
subject.style.borderColor = borderColorOver;
}
}
function heartMouseOut(a) {

if (imgClicked < 1) {
var images = document.getElementById(a);
images.style.borderColor = borderColorOff;
var subject = document.getElementById('s'+a);
subject.style.borderColor = borderColorOff;
}
}
</script>


<body>

<div class="pictureBox">
<div class="picture">
<img onmouseover="heartMouseOver(this.id)" onmouseout="heartMouseOut(this.id)" id="0" onclick = "heartMouseClick(this.id)" class="images" border="5" src="1.jpg">
</div>
<div class="subject" id="s0" border ="5">
Image 0 Description
</div>
</div>
<div class="pictureBox">
<div>
<img onmouseover="heartMouseOver(this.id)" onmouseout="heartMouseOut(this.id)" id="1" onclick = "heartMouseClick(this.id)" class="images" border="5" src="emergency.jpg">
</div>
<div class="subject" id="s1" border ="5">
Image 1 Description
</div>
</div>
<div class="pictureBox">
<div>
<img onmouseover="heartMouseOver(this.id)" onmouseout="heartMouseOut(this.id)" id="2" onclick = "heartMouseClick(this.id)" class="images" border="5" src="lebron.jpg">
</div>
<div class="subject" id="s2" border ="5">
Image 2 Description
</div>
</div>

</body>
</html>

您走在正确的轨道上,我只是添加了 id 并传递了它们。

关于Javascript 图像 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39171493/

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