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

给动态生成DIV赋值的问题
前台代码
  <script language=javascript type="text/javascript">
  var divId=1;
  //动态生成单纯的div
  function CreateOuterDiv()
  {
  var obj=document.createElement("div");
  obj.id="myDiv"+divId;
  divId++;
  obj.style.border="1px solid #000000";
  obj.style.height="30px";
  obj.style.width="200px";
  obj.style.filter="alpha(opacity=70)";  
  obj.style.margin="10px";  
  obj.style.cursor="hand";
  obj.algin="center";  
  //obj.innerHTML="<a href='#"+obj.id+"'>ssssssssss</a>";
  obj.innerText="strname";
   
  document.body.appendChild(obj);  
  document.getElementById(obj.id).onclick=function(){aa();};  
  }
  function aa()
  {
  CreateOuterDiv();//alert();
  }  
  </script>

后台
  ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript ", "<script type='text/javascript'>CreateOuterDiv();</script> ");

生成的DIV 显示的是 strname

我想要的是 让obj.innerText="strname";显示我要显示的值 strname 是后台赋予的一个变量值

------解决方案--------------------
function CreateOuterDiv(strname)
obj.innerHTML=strname;

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript ", "<script type='text/javascript'>CreateOuterDiv('" + "后台变量" + "');</script> ");
------解决方案--------------------
function CreateOuterDiv()
=>
 function CreateOuterDiv(text)
------------------------------------------
obj.innerText="strname";
=>
obj.innerText=text;
----------------------------------
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript ", "<script type='text/javascript'>CreateOuterDiv();</script> ");
=>
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript ", "<script type='text/javascript'>CreateOuterDiv('"+strname+"');</script> ");






------解决方案--------------------
innerText
非IE不支持,请换成innerHTML
------解决方案--------------------
上面测试通过。