为什么在asp.net中需要RegisterStartupScript来实现简单的jquery helloworld?
我试图通过在webform中包含javascript来调用我的helloworld,但是当运行它时,chrome和firefox上的页面都是空白的。 在firefox错误是
“XML解析错误:找不到任何元素”
function helloWorld() { $("#divSample").append("Hello World!!"); }
我需要添加codebehind:
protected override void Render(HtmlTextWriter writer) { this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "startup", "helloWorld();"); base.Render(writer); }
在那种情况下它可以工作,但我不明白为什么我不能使用第一种语法为什么它如此简单的这么简单的东西?
我也尝试了这个建议,但它也没有用:
function helloWorld() { $("#divSample").append("Hello World!!"); } $(document).ready(function () { helloWorld(); });
更新:似乎ASP.NET在某些情况下使用ajax / jquery是不可靠的? http://chiragrdarji.wordpress.com/2010/02/17/xml-parsing-error-no-element-found/
尝试用jQuery $(document).ready语法包装你的helloWorld()调用:
您是否尝试在调试器中运行它,例如FireBug for FIreFox或Chrome的开发人员工具? (还没有尝试过IE浏览器……我复制并粘贴了上面的代码,对我来说效果很好……
尝试在脚本标记上使用delay="delay"
属性。
如果您仍在渲染html时尝试修改DOM结构,IE不喜欢。 此属性告诉浏览器延迟脚本执行,直到完成渲染。
你试过摆脱Render
覆盖吗?
protected override void Render(HtmlTextWriter writer) { this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "startup", ""); base.Render(writer); }