gpt4 book ai didi

javascript - 鼠标悬停时更改图片

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

早安世界!我的项目遇到了另一个问题,这次 JavaScript 无法正常工作,我希望当我将鼠标指针悬停在图片上时图片会发生变化,感谢您的耐心等待!

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css" type="text/css" />
<title>Konst UF</title>
<meta charset="utf-8" />
<script type="text/javascript" language="javascript">
if (screen.width <= 699) {
document.location = "mobile.html";
}

document.getElementById("Orderbutton").onclick = function () {
location.href = "http://www.youtube.com/";
};

function billFunction() {
var Bill = getElementById("BillGate");
if (img.src.match("Bill")) {
img.src = "bill-gates.jpg";
} else {
img.src = "Card.jpg";
}
}
</script>
<body>
<p class="madeby">Made by Albin Karlsson and Oliver Jansson</p>
<center>
<ul>
<li>
<a href="index.html"><p class="Home">Home</p></a>
</li>
<li>
<a href="#"><p class="Products">Products</p></a>
</li>
<li>
<a href="#"><p class="About">About Us</p></a>
</li>
</ul>
</center>
<center><p class="para1">Konst UF</p></center>
<center>
<img
id="BillGate"
src="bill-gates.jpg"
alt="Bill Gates"
class="Billgates"
onmouseover="billFunction()"
/>
</center>
<marquee><h2>Bill GATES</h2></marquee>
<div></div>
<div class="sidepanels1">
<center>
<img class="Konstbild" src="Konst_uf_1.jpg" alt="Konst" />
</center>
<h2>Unknown</h2>
<p>I have no idea haha</p>
</div>
<div class="sidepanels2">
<center>
<img class="Konstbild" src="mikrofiberduk-konst.jpg" alt="Monalisa" />
</center>
<center><h2>Mona Lisa</h2></center>
<center>
<p>Mona Lisa is a painting which was painted by Leonardo Da Vinci</p>
</center>
</div>
<center>
<button
id="Orderbutton"
type="button"
onclick="location.href = 'http://www.youtube.com/';"
>
Order Our Products
</button>
</center>
</body>
</head>
</html>

那是 html 代码,我希望你能找到问题所在,如果你需要 css,我也有:

body {
background-color: grey;
border: grey solid 1px;
}

p.para1 {
margin: auto;
width: 40%;
border: 3px solid black;
padding: 20px;
background-color: black;
color: white;
font-size: 30px;
}
div.sidepanels1 {
border: 5px dotted green;
background-color: grey;
position: absolute;
top: 10px;
right: 10px;
width: 320px;
height: 550px;
text-align: center;
margin-top: 40px;
}
div.sidepanels2 {
border: 5px dotted green;
background-color: grey;
position: absolute;
top: 10px;
left: 10px;
width: 320px;
height: 550px;
margin-top: 40px;
}
p.iframe {
position: absolute;
top: 20px;
text-align: center;
}
div.2nd {
background-color: black;
color: white;
}
img.asus-logo {
width: 1200px;
margin-left: auto;
margin-right: auto;
border: 2px solid black;
}
img.Billgates {
margin-top: 30px;
border: 2px solid black;
margin-bottom: 40px;
}
img.Konstbild {
margin-top: 20px;
width: 300px;
height: 300px;
border: 3px solid green;
margin-right: auto;
margin-left: auto;
}
ul {
list-style-type: none;
}
p.madeby {
position: fixed;
}

谢谢!

最佳答案

让我们看一下您的 billFunction,因为此函数存在一些问题。

function billFunction() {
var Bill = getElementById('BillGate');
if (img.src.match("Bill")) {
img.src = "bill-gates.jpg";
} else {
img.src = "Card.jpg";
}
}

首先,获取您想要的元素需要您使用document.getElementById,而不仅仅是getElementById。更改它应该会给你正确的元素。

接下来,您要设置 img.src,但从 undefined variable img。我假设这应该是 Bill.src。 (旁注,我想建议您使用 lowerCamelCase 变量名)

最后,您检查使用哪个图像的逻辑看起来不对。 Bill(大写 B)永远不会出现在 bill-gates 中。使用全部小写更改此逻辑应该可行。

您将获得以下内容:

function billFunction() {
var bill = document.getElementById('BillGate');
if (bill.src.match("bill")) {
bill.src = "bill-gates.jpg";
} else {
bill.src = "Card.jpg";
}
}

关于javascript - 鼠标悬停时更改图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36005882/

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