简体   繁体   English

我不知道这个简单的 javascript 代码有什么问题,我一遍又一遍地检查了这个但不起作用

[英]I dont know whats wrong with this simple javascript code, I have checked this over and over again but does not work

Simple image expanding on mouseover and opposite on mouseout, I checked the code over and over, putting alert messages at each line to see if its working, alert message work all the way to the end but there is no effect on the image.简单的图像在鼠标悬停时展开,在鼠标悬停时相反,我一遍又一遍地检查代码,在每一行放置警报消息以查看其是否工作,警报消息一直工作到最后,但对图像没有影响。 I am so confused, I dunno whats wrong.我很困惑,我不知道怎么了。

 var side = 200; var a = 20; var t = 0; function expand() { console.log("expand is ok"); clearInterval(t); t = setInterval(grow, 20); } function grow() { console.log("Entered grow"); if (side < 300) { console.log("entered if loop"); side = side + a; document.getElementById("new").style.width = side; console.log("After statement") } else clearInterval(t); console.log("clear") }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Zoom in Zoom out</title> </head> <body> <img src="../images/women.jpg" id="new" alt="Women" onmouseover="expand()" width=2 00px> </body> </html>

To expand on my comment:扩展我的评论:

The issue is you're trying to assign a raw number such as 200 to the width CSS property.问题是您尝试将原始数字(例如200 )分配给宽度 CSS 属性。

If you look at CSS, you'd spell "200 pixels" as如果您查看 CSS,您会将“200 像素”拼写为

foo {
  width: 200px;
}

so similarly you'll need to endow the dynamic manipulation with an unit (pixels here):所以同样你需要赋予动态操作一个单位(这里的像素):

foo.style.width = `${width}px`;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我已经尝试使用ajax调用我的PHP函数,但我不知道它的代码错误是什么不起作用 - I have tried using ajax to call into my php function but i dont know whats wrong with the code its not working 一个简单的 JS 代码,我一遍又一遍地收到相同的结果,我想,我可能犯了一个逻辑错误, - A simple JS code and I am receiving the same result over and over again, I think, I may have done a logical error, 我知道此代码无法正常工作,为什么我找到的代码可以工作? 是什么使错误的代码错了? - I know this code does not work, why does the code that I found does? What makes the wrong code, wrong? 我不知道为什么JavaScript会这样做 - I dont know why JavaScript does this 此代码有什么问题? .checked可能是我没有正确使用的那个 - Whats wrong with this code? .checked is probably the one i am not using correctly 我不知道为什么我的代码是错误的? 有什么问题吗? - I dont know why my code is wrong ? and what wrong is it? 我是否在此jQuery代码中一再绑定事件? - Am I binding events over and over again in this jQuery code? JavaScript,有人知道这段代码有什么问题吗? - JavaScript, anyone know whats wrong with this code? 我有一个简单的网站,想在鼠标悬停时更改字体大小,但此代码不起作用。为什么? - I have a simple site and want to change my font-size when the mouse is over but this code don't work.Why? puppeteer node.js 代码不起作用。 我不知道为什么 - puppeteer node.js code does not work. and i dont know why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM