日期:2014-05-17  浏览次数:20502 次

Page.ClientScript.RegisterStartupScript在循环中只执行一次?
后台这么写:
for(int i;i<5;i++){
string getid="span"+i+";
string jsq="<script>show('"+getid+"');</script>";
Page.ClientScript.RegisterStartupScript(this.GetType(),"key",jsq);}
前台JS:
function show(id)
    {
        document.getElementByID(id).title="哈哈";
    //alert(info);
    }
function show1()
    {    
alert("haha");
    }

前台代码
<span id="span1"></span>
<span id="span2"></span>
<span id="span3"></span>
<span id="span4"></span>

为什么只有span1 的title被赋值了,而其他的span没有呢?我想要的效果是全部被赋值了,请问应该如何操作呢?
还有一个问题,我用response.write("<script>show1();</script>");这种方式调用前台Js方法,为什么在有的界面就好用,而有的界面不好用呢?其中好用的也是调用的前台自定义的方法,而不是系统带的脚本方法。

------解决方案--------------------

Page.ClientScript.RegisterStartupScript(this.GetType(),"key",jsq);}
关键在这个key上,
这个是脚本的关键字,虽然执行了四次,但是四次脚本的关键字相同,所以在页面只有一个,你可以

Page.ClientScript.RegisterStartupScript(this.GetType(),"key"+i,jsq);}
这样应该没问题
------解决方案--------------------
ClientScript.RegisterStartupScript这个是在js之后执行的
response.write这个是加载之前执行的  在js之前

你应该在js方法里show  给4个span循环赋值好