DHTML中使用attachEvent方法的问题小结

Window对象的attachEvent方法

将指定函数绑定到事件,以便每当该事件在对象上触发时都调用该函数。

<SCRIPT LANGUAGE="JScript">
attachEvent ('onmouseover', Hilite);
attachEvent ('onmouseout', Restore);
function cleanup()
{
detachEvent ('onmouseover', Hilite);
detachEvent ('onmouseout', Restore);
}
function Hilite()
{
if (event.srcElement == element)
{
normalColor = style.color;
runtimeStyle.color  = "red";
runtimeStyle.cursor = "hand";
}
}
function Restore()
{
if (event.srcElement == element)
{
runtimeStyle.color  = normalColor;
runtimeStyle.cursor = "";
}
}
</SCRIPT>


1、CS文件中构造组合这样与便执行的JS代码
JStr02 += "document.getElementById('txt_AreaCityName').attachEvent('onfocus',MyOnfocus);";
JStr02 += "document.getElementById('txt_AreaCityName').attachEvent('onmouseover',MyOnmouseover);";
JStr02 += "document.getElementById('txt_Author').value ='';";//清空作者
JStr02 += "document.getElementById('txt_AreaCityName').attachEvent('onblur',new Function('MyOnblur(1);'))";

注意怎么使用attachEvent调用带参数的函数的方法可以参考上面红色的这句

找了半天找到的解决办法,看介绍说是javascript的闭包问题,导致得不能直接读取外部的那个函数,不然就所有传递的参数都变为最后一个了。

var newopen = function(id,level)
{
  
return function()
  {
    opentree(id,level);
//该函数为外部定义的一个执行函数;
  }
}
x.attachEvent(
"onclick",newopen(id,parseInt(level)+1));
y.attachEvent("onclick",newopen(id,parseInt(level)+2));


2、
        /// <summary>
        /// 执行客户端脚本
        /// </summary>
        /// <param name="currentPage">Page</param>
        /// <param name="strstr">脚本内容</param>
        public static void PageExcecScript(System.Web.UI.Page currentPage, string strstr)
        {
            //脚本块的内容
            //先将提示信息中的某些字符做转换,否则会影响脚本的执行
            string strDescription = "";
            strDescription = strstr.Replace("\"", "
\\\"");
            strDescription = strstr.Replace("\\", "
\\\\");
            strDescription = strstr.Replace("\r", "
\\r");
            strDescription = strstr.Replace("\n", "
\\n");

            string csname = Guid.NewGuid().ToString();//随机定义脚本块名称
            ClientScriptManager cs = currentPage.ClientScript;
            if (!cs.IsStartupScriptRegistered(currentPage.GetType(), csname))
            {
                string cstext = strDescription;
                cs.RegisterStartupScript(currentPage.GetType(), csname, cstext, true);
            }
        }

3、前台ASPX中使用这样的JS
 <script type ="text/javascript">
   function MyOnfocus()
   {
      //event.srcElement.value="11";
     event.srcElement.select();
   }
   function MyOnmouseover()
   {
      event.srcElement.focus();
   }
   function MyOnblur(state)
   {
       switch (state)
       {
            case 1:
            if (event.srcElement.value =='') event.srcElement.value='填写展览城市名字';
            break;
             case 2:
            if (event.srcElement.value =='') event.srcElement.value='请输入关键字';
            break;
            default :
             if (event.srcElement.value =='') event.srcElement.value='请按要求填写';
       }
   }
   </script>

************************************

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
</head>

<body>
<a href="#" id="mylink">这是连接</a>
<script type="text/javascript">
function AttachEvent(type, target, handler, owner)
{
        var eventHandler = handler;
    if(owner)
    {
            // 在这里重新定义一个处理函数
            eventHander = function(e)
            {
                    handler.call(owner, e);
            }
    }
    if(window.document.all)
            target.attachEvent("on" + type, eventHander );
    else
            target.addEventListener(type, eventHander, false);
}

function MyLink_Click(e)
{
        alert(this.name);
        var target = e.srcElement || e.target;
        alert(target.href);
}

var myobj = new Object();
myobj.name = "这是我的名字";

var mylink = window.document.getElementById("mylink");


AttachEvent("click", mylink, MyLink_Click, myobj);

</script>
</body>

</html>

posted @ 2008-02-18 13:00  nestea  阅读(441)  评论(0编辑  收藏  举报