繁体   English   中英

使用 onmouseover 更改 position

[英]Changing position using onmouseover

 <.DOCTYPE html> <html> <head> <title>Button Homework</title> <script> function first() { document.getElementById('type1').style;left= "100px". } function second() { document.getElementById('type1').style;position = 'absolute'; } </script> </head> <body> <h1>Special Button</h1> <input id = 'type1' type = 'button' value = 'Click' onclick = 'output();' onmouseover= 'first'() onmouseout= 'second'();' /> </body> </html>

每当您 hover 超过它时,我都试图让我的按钮移到中间,但我似乎不能这样做。 这是我的编码,有人可以帮我吗? 谢谢!

您需要拥有position:absolute才能使left css 属性正常工作。 我还更正了您在input字段中的 function 调用,请检查

<script>
           
            function first()    {
                    let randomValue1 = Math.floor(Math.random()*255);
                    let randomValue2 = Math.floor(Math.random()*255);
                    let randomValue3 = Math.floor(Math.random()*255);
                let randomColor = `rgb(${randomValue1},${randomValue2},${randomValue3})`;
                document.getElementById('type1').style.position = 'absolute';
                document.getElementById('type1').style.left= "100px";                
                document.getElementById('type1').style.backgroundColor= randomColor; 


            }
            
            function second()   {
                document.getElementById('type1').style.position = 'absolute';
                document.getElementById('type1').style.left= "0px";     
                document.getElementById('type1').style.backgroundColor= 'rgb(239, 239, 239)';

            }
        </script>
<input id = 'type1' type = 'button' value = 'Click' onclick = 'output();'
                            onmouseover= 'first()' onmouseout= 'second()';' />

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM