Windows.event未定义 - Firefox中的JavaScript错误

Dev*_*404 4 javascript asp.net onmouseover

我正在使用javascript来更改鼠标悬停时asp按钮的某些设置.它在IE中工作.但不适用于Firefox.还有其他任何支持几乎所有浏览器的javascript代码吗?我的代码如下

<script type="text/javascript">
        var previousColor;
        function Changecolor() {
            previousColor = window.event.srcElement.style.backgroundColor;
            window.event.srcElement.style.backgroundColor = "Blue";
            window.event.srcElement.style.cursor = "hand";
        }
        function RestoreColor() {
            window.event.srcElement.style.backgroundColor = previousColor;
        }
</script>


<asp:Button ID="btnSearch" runat="server" BackColor="#800000" Font-Bold="True" Font-Names="Arial" onmouseover="Changecolor();" onmouseout="RestoreColor();" ForeColor="White" Height="28px" OnClick="btnSearch_Click2" Text="Search Jobz" Width="117px" />
Run Code Online (Sandbox Code Playgroud)

And*_*y E 8

查看有关事件的Mozilla开发人员中心文档.在Internet Explorer中,触发事件时会创建全局事件对象.在符合标准的浏览器中,事件对象作为分配给触发事件的函数的第一个参数传递.如果您的事件是在HTML中定义的,则事件对象是在变量名下创建的,event并且可以传递给您正在调用的函数.

另请注意,该event.srcElement属性仅适用于IE,而大多数其他浏览器则使用该属性event.target.

考虑到这一点,您的功能应如下所示:

<script>
        var previousColor; 
        function Changecolor(evt) {
            var srcEl = evt.srcElement || evt.target;
            previousColor = srcEl.style.backgroundColor; 
            srcEl.style.backgroundColor = "Blue"; 
            srcEl.style.cursor = "pointer"; 
        } 
        function RestoreColor(evt) {
            var srcEl = evt.srcElement || evt.target;
            srcEl.style.backgroundColor = previousColor; 
        } 
</script> 


<asp:Button ID="btnSearch" runat="server" BackColor="#800000" Font-Bold="True" Font-Names="Arial" onmouseover="Changecolor(event);" onmouseout="RestoreColor(event);" ForeColor="White" Height="28px" OnClick="btnSearch_Click2" Text="Search Jobz" Width="117px" />
Run Code Online (Sandbox Code Playgroud)

  • 好的..我使用"指针"代替"手"..现在它对我有用:-)非常感谢... (2认同)