前台:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(function () {
function func() {
alert("yes");
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
后台就写了一句:
protected void Button1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>func()</script>");
}不明白怎么会出现这种情况,大家帮忙看看吧!多谢了!
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(function () {
function func() {
alert("yes");
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
后台就写了一句:
protected void Button1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>func()</script>");
}不明白怎么会出现这种情况,大家帮忙看看吧!多谢了!
function func() {
alert("yes");
}
</script>
按你说的做确实不会报错,但我不明白为什么这样做就不会报错,我把func这个js函数放在jquery的$(document).ready中为什么会报错,为什么找不到func这个js函数?
{
if (!IsPostBack)
{
void LoadData()
{
Response.Write("Test");
}
}
}
反正我是不会这样的,一样的道理。
你说的意思我知道,我不明白的是为什么RegisterStartupScript这个方法找不到func这个函数?如果不把它放在$(document.ready)中就能找到,我不明白为什么会这样?能说详细点吗?谢谢!
$(document).ready也是一个函数。函数套函数??
$(function() {
function func() {
alert("yes");
}
$("#Button1").click(function() {
func();
});
});
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript"> function func() {
alert("yes");
} </script>
function func() {
alert("yes");
}
});楼主可能初学jQuery吧,
初学这个的人,都很喜欢用$(function(){ xxxxx });
这个东东一来相当于onload,二来它是个闭包,里面定义的东东外面访问不到。而你在后台写的调用func(),当然也是不行。有空再多看看javascript的基础书籍吧,不要觉得jQuery就是一切。