Windows.event未定义-Javascript错误在Firefox [英] Windows.event is undefined -Javascript error in firefox

查看:281
本文介绍了Windows.event未定义-Javascript错误在Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JavaScript来改变鼠标悬停ASP按钮的一些设置。它工作在IE浏览器。但不是在Firefox工作。是否有任何其他的JavaScript code将支持几乎所有的浏览器?我的code是如下:

 <脚本类型=文/ JavaScript的>
        VAR previousColor;
        功能Changecolor(){
            previousColor = window.event.srcElement.style.backgroundColor;
            window.event.srcElement.style.backgroundColor =蓝;
            window.event.srcElement.style.cursor =手;
        }
        功能RestoreColor(){
            window.event.srcElement.style.backgroundColor = previousColor;
        }
< / SCRIPT>
< ASP:按钮的ID =btnSearch=服务器背景色=#800000FONT-粗体=真FONT-名称=宋体的onmouseover =Changecolor();的onmouseout =RestoreColor();前景色=白HEIGHT =28px的OnClick =btnSearch_Click2文本=搜索JobzWIDTH =117px/>


解决方案

看看在事件Mozilla开发者中心文档。在Internet Explorer中,当事件被激活时创建的全局事件对象。在标准兼容的浏览器,该事件对象作为分配给该点火事件的函数的第一个参数传递。如果你的事件在HTML定义,在变量名事件创建事件对象,可以传递给你打电话的功能。

另外请注意, event.srcElement 属性是只有IE浏览器和大多数其他浏览器使用 event.target 来代替。

考虑到这一点,你的函数应该是这样的:

 <脚本>
        VAR previousColor;
        功能Changecolor(EVT){
            VAR srcEl = evt.srcElement || evt.target;
            previousColor = srcEl.style.backgroundColor;
            srcEl.style.backgroundColor =蓝;
            srcEl.style.cursor =指针;
        }
        功能RestoreColor(EVT){
            VAR srcEl = evt.srcElement || evt.target;
            srcEl.style.backgroundColor = previousColor;
        }
< / SCRIPT>
< ASP:按钮的ID =btnSearch=服务器背景色=#800000FONT-粗体=真FONT-名称=宋体的onmouseover =Changecolor(事件);的onmouseout =RestoreColor(事件);前景色=白HEIGHT =28px的OnClick =btnSearch_Click2文本=搜索JobzWIDTH =117px/>

I'm using javascript to change some settings of asp button on mouseover. It is working in IE. But not working in Firefox. Is there any other javascript code that will support almost all browsers? My code is as follows

<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" />

解决方案

Take a look at the Mozilla Developer Center docs on events. In Internet Explorer, the global event object is created when an event is fired. In standards compliant browsers, the event object is passed as the first argument of the function assigned to the firing event. If your event is defined in the HTML, the event object is created under the variable name event and can be passed to the functions you're calling.

Also note that the event.srcElement property is IE only and most other browsers use event.target instead.

Taking this into consideration, your function should look like this:

<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" />

这篇关于Windows.event未定义-Javascript错误在Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆