JS修改當前控制元件樣式&為控制元件追加事件

劍握在手發表於2013-08-25

先擱這吧,今天太晚了,以後再加註釋和修整吧。不幸搜到的朋友就別看了

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript">
        function set() {
            var obj = document.getElementById("fy");

            //obj.attachEvent('onfocus', add); //在原先事件上新增
            //obj.setAttribute('onfocus',add); //會替代原有事件方法
            //obj.onfocus=add;                //等效obj.setAttribute('onfocus',add);        

            if (window.addEventListener) {
                //其它瀏覽器的事件程式碼: Mozilla, Netscape, Firefox
                //新增的事件的順序即執行順序 //注意用 addEventListener 新增帶on的事件,不用加on 
                obj.addEventListener('focus', add, false);
            }
            else {
                //IE 的事件程式碼 在原先事件上新增 add 方法
                obj.attachEvent('onfocus', add);
            }
        }

        function func(mess) {
            alert(mess);
        }

        function changecss(e) {
            if (e.className = "redcss") {
                e.className = "likelink";
                e.attachEvent('onclick', function () { func("2321"); }); //帶引數
                e.onmouseover = add

                // e.addEventListener('mouseover', add, false);

                
            }
        }
        function add() {
            
        }
    </script>
    <style type="text/css">
        .redcss {
            color:red
        }
        .likelink {
            width:20px;
            color:blue;
            text-decoration:; 
            cursor:pointer;        }
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="width:30px">
            <a id="link" class="redcss" runat="server" onclick="changecss(this)"><h1>good</h1> </a>
        </div>
    </form>
</body>
</html>

 

相關文章