收集的一些JS的代码段

来源:互联网 发布:com.cn和cc域名哪个好 编辑:程序博客网 时间:2024/05/17 21:15

----锚点
window.locaion.hash="ST1";

<a name="ST1"> </a>


----焦点
var fc=document.getElementById(id);
fc.focus();


----页面加载时执行
function window.onload()  
{
     self.parent.scrollTo (0,0);   //在iframe中控制主页面的滚动条
      //若干操作...   
}

-----获取后台DropDownList中的值
var strValue=document.getElementById(ParentID).options[document.getElementById(ParentID).selectedIndex].value



----获取页面对象
function $()
{
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++)
    {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}

----闪烁文字
     colors2 = new Array(6);
     colors2[0]="#fef4d9";
     colors2[1]="#333300";
     colors2[2]="#665500";
     colors2[3]="#997700";
     colors2[4]="#CC9900";
     colors2[5]="#22ff55";
     var i=0;
     function fLi2() {
             $("line2").style.visibility = "visible";
             if (i<6) {
                     $("line2").style.color = colors2[i];
                     i++;
                     timerID2 = setTimeout( "fLi2()", 40);
             }
             else {
             i=0;
             TimerID2=setTimeout("fLi2()",200);
             }
       }

----应用程序启动时运行
window.setTimeout("AjaxInvoke()",9000); //方法名,等待时间(毫秒)

----C#后台调用前台javascript的五种方法

由于项目需要,用到其他项目组用VC开发的组件,在web后台代码无法访问这个组件,所以只好通过后台调用前台的javascript,从而操作这个组件。在网上找了找,发现有三种方法可以访问到前台代码:

第一种,OnClientClick    (vs2003不支持这个方法)
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="client_click()" OnClick="Button1_Click" />
client_click() 就是javascript的一个方法。

第二种,Button1.Attributes.Add("onclick", "return Client_Click()");
“Client_Click() “是一个前台方法,可以替换成一般的脚本如:retrun confirm('确定删除吗?')

第三种,是我自认为最灵活的一种,ClientScript.RegisterStartupScript
例子:StringBuilder sb = new StringBuilder();
        sb.Append("<script language='javascript'>");
        sb.Append("Button2_onclick('" + serverPath + "')");
        sb.Append("</script>");
        ClientScript.RegisterStartupScript(this.GetType(), "LoadPicScript", sb.ToString());

 

第四种. 用Response.Write方法写入脚本

比如在你单击按钮后,先操作数据库,完了后显示已经完成,可以在最后想调用的地方写上
Response.Write("<script type='text/javascript'>alert();</script>");

这个方法有个缺陷就是不能调用脚本文件中的自定义的函数,只能调用内部函数,具体调用自定义的函数只能在Response.Write写上函数定义,比如Response.Write("<script type='text/javascript'>function myfun(){...}</script>");

第五种 用ClientScript类动态添加脚本

    用法如下:在想调用某个javascript脚本函数的地方添加代码,注意要保证MyFun已经在脚本文件中定义过了。

    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>MyFun();</script>");

这个方法比Response.Write更方便一些,可以直接调用脚本文件中的自定义函数。

 

可以在程序的任何地方执行,o(∩_∩)o...,是不是很好用呢

注意执行顺序:先执行Client ,再执行Server

 

//居中弹出窗体
        function getwindow(URL,width,height)
        {
        //根据屏幕居中
          window.open(URL,'','width='+width+',height='+height+',top='+(screen.height-height)/2+',left='+(screen.width-width)/2);
        //下面是根据页面居中
        //window.open(URL,'','width='+width+',height='+height+',top='+(document.body.offsetHeight-height)/2+',left='+(document.body.offsetWidth-width)/2);
        //window.open(URL,'','width='+width+',height='+height+',top=250,left='+document.body.offsetWidth/3);
        }

原创粉丝点击