onfocus和onblur仅适用于IE7

时间:2013-03-24 12:20:18

标签: javascript asp.net css

我们可以为IE7等特定浏览器编写onbluronfocus等事件吗?

我尝试过的是下面的内容。

<asp:TextBox  onfocus="this.style.backgroundColor='yellow'" onblur="this.style.backgroundColor='white'" Text="something" ID="txtName" runat="server" ></asp:TextBox>

请注意,这对我来说很好。但我只需要onbluronfocus来执行IE7。

这可能吗?

也许某人已经面临这种需要。

提前致谢

1 个答案:

答案 0 :(得分:1)

您可以检测浏览器并仅在IE7

时运行您的脚本

将它放在标记正文标记的末尾

<!--[if lte IE 7]>
    <script type="text/javascript">
        var txtBox = document.getElementById('<%= txtName.ClientID %>');
         txtBox.onfocus =function(){ txtBox.style.backgroundColor='yellow';};
         txtBox.onblur=function(){txtBox.style.backgroundColor='white';};
    </script>
<![endif]-->

或将其放在头部

<!--[if lte IE 7]>
    <script type="text/javascript">
      window.onload=function(){ 
           var txtBox = document.getElementById('<%= txtName.ClientID %>');
           txtBox.onfocus =function(){ txtBox.style.backgroundColor='yellow';};
           txtBox.onblur=function(){txtBox.style.backgroundColor='white';};
     }
    </script>
<![endif]-->