简体   繁体   English

Javascript函数无法使用onmouseover属性

[英]Javascript function is not working with onmouseover attribute

I want to change css property when mouse is over/out on images. 当鼠标悬停在图像上时,我想更改css属性。 Unfortunately, it didn't seem to work, so I tested simple javascript with onmouseover attribute: 不幸的是,它似乎没有用,所以我用onmouseover属性测试了简单的javascript:

<script type="text/javascript">
    function show_alert() { document.alert("Alert"); }
</script>

and

<div class="test" onmouseover="show_alert()">
  ....
</div>

but when I moved mouse over this div block, nothing happened and kinda hard to debug what was going inside. 但是当我将鼠标移到该div块上时,什么也没发生,也很难调试内部的内容。 How can I resolve this problem ? 我该如何解决这个问题?

The alert doesn't work because you're using document.alert , which doesn't exist. 该警报不起作用,因为您使用的是document.alert ,该document.alert不存在。 You'll be wanting window.alert , or just alert 您将需要window.alert ,或者只是alert

Check out this JSFiddle 看看这个JSFiddle

The alert method is a method of the window object, not the document object. alert方法是window对象的方法,而不是document对象的方法。 Using document.alert returns undefined . 使用document.alert返回undefined

<script type="text/javascript">
  function show_alert() { window.alert("Alert"); }
</script>

I want to change css property when mouse is over/out on images 当鼠标悬停在图像上时,我想更改css属性

why do you need javascript for this? 为什么您需要使用JavaScript? A simple CSS Style does the trick: 一个简单的CSS样式就能解决问题:

img:hover { border: 2px solid red; }

By the way 顺便说说

document as no property called alert . document没有属性,称为alert It's window.alert() ! 这是window.alert() or simply alert('hello'); 或简单地alert('hello');

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

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