gpt4 book ai didi

javascript - 单击更改图像的位置

转载 作者:行者123 更新时间:2023-11-27 22:45:45 27 4
gpt4 key购买 nike

   <html>
<head>
<script type="text/javascript">
function changepic()
{
document.getElementById("demo").style.top=2000;
}
</script>
</head>
<body>

<h1>My First Web Page</h1>
<div id="demo">
<img src="./omna.jpg" style="position:absolute; left: 400; top: 100; width: 200; height: 200;"/>
</div>
<button type="button" onclick="changepic()">change pic</button>
</body>
</html>

为什么我不能改变图片的位置出了什么问题?

<img id="demo"> ' google says it works

我不知道,但上面的 html 页面无法正常工作 我想在单击按钮时更改图像的位置,请帮我解决这些问题。我正在使用谷歌浏览器

<img onclick="somefunc()"> can I set function onclick to image also? right?

但它不起作用请帮我处理 html 代码!

最佳答案

您正在尝试设置 top ID 为 demo 的元素,这是一个 <div> .我假设 div 没有 position:absolute放在上面,所以不会移动到任何地方,我猜你想要 <img>移动而不是 <div>反正。如果您从 <div> 中删除 id并将其添加到 <img> (就像上面的第二段代码一样)你不应该有任何问题。此代码应该有效:

<html>
<head>
<script type="text/javascript">
function changepic()
{
document.getElementById("demo").style.top=2000 + "px";
}
</script>
</head>
<body>

<h1>My First Web Page</h1>
<div>
<img id="demp" src="./omna.jpg" style="position:absolute; left: 400; top: 100; width: 200; height: 200;"/>
</div>
<button type="button" onclick="changepic()">change pic</button>
</body>
</html>

关于javascript - 单击更改图像的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455893/

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