后台代码:
public string aaa()
{
string a = "qqqqq";
return a;
}
前台代码:
<script>var a = <%=aaa() %>; alert(a)</script>
为什么不会输出?
谢谢!!!那如果是个数组呢?
后台代码:
public Array getUrl()
{
string[] a={ "031.jpg","033.jpg","047.jpg"};
return a.ToArray();
}
前台代码:
<img id="p" />
<script>
var t = new Array();
t=<%=getUrl()%>;
var i = 0;
function changepic() {
if (i > 2)
i = 0;
var p = document.getElementById("p");
p.src = t[i]; i++;
setTimeout(changepic, 4000);
}
window.onload = changepic;
</script>
数组的话,可以这么写:
public string getUrl() { string[] a = { "031.jpg", "033.jpg", "047.jpg" }; return string.Join("','",a); }
页面:
<img id="p" /> <script> var t = new Array(); t=['<%=getUrl()%>']; var i = 0; function changepic() { if (i > 2) i = 0; var p = document.getElementById("p"); p.src = t[i]; i++; setTimeout(changepic, 4000); } window.onload = changepic; </script>
总之就是让getUrl输出你页面想要的那部分js代码,最好的调试方法就是查看生成后的html,你就清楚了
很简单,页面的代码变成了 var a = qqqqq; 缺少引号
应该改为:<script>var a = "<%=aaa() %>"; alert(a)</script>
学习了