js的Cannot set c getproperty null'innerHTML' of null 问题

ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法
字体:[ ] 类型:转载 时间:
同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误
Chrome提示:
确定相关JS已经包含在页面中,(用的是MasterPage,二个页面包含的js文件完全相同),排除了js引用路径不正确的问题。
页面上生成的mvcClientValidationMetadata也没问题,但客户端验证就是不起作用。
将MicrosoftAjax.js替换成MicrosoftAjax.debug.js。在Chrome调试工具中看到此异常在Sys.UI.DomElement.getElementById函数中抛出
在394行加个断点,刷新页面,一步步跟踪,
162行,根据ID获取某个DOM对象,在这里看到optionsFormID为null。(Chrome的js调试功能很强),再看到CallStack的上一步。
这里是在页面加载完成后处理客户端验证的mvcClientValidationMetadata 数据。
跟到这里大概能就猜出问题在哪里了。&
查看页面源码,在&/form&节点后的脚本中看到生成的验证代码如下:
表单的ID为null!
然后我再打开难进行客户端验证的页面:
看到差别了,就是因为FormID为null。&
并且他们生成的表单也有所不同:
不能进行客户端验证的Form
能进行客户端验证的Form
有id的Form能进行客户端验证!
可是二个视图中都是用using (Html.BeginForm()){}生成表单的,没有特意去设置Form ID属性,为何一个有id另一个没id?
打开二个视图页面,一个一个排查,最后发现二个视图的差别在这一点:
前都是在BeginForm()之前调用Html.EnableClientValidation();,后BeginForm之后才调用 Html.EnableClientValidation();&
记住了,想要用mvc client side validation,请在BeginForm()之前调用Html.EnableClientValidation();
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具js编程浏览器总是报错 Uncaught TypeError: Cannot set property &innerHTMl& of null
js编程浏览器总是报错 Uncaught TypeError: Cannot set property &innerHTMl& of null
我的源码是:&body & &div id=&restt&&&/div&
&script type=&text/javascript&& window.document.getElementById(&restt&).innerHTMl=&abc&;Uncaught TypeError: Cannot set property 'innerHTMl' of null &/script& &/body &
&div id=&restt&&&/div& 里面没有元素; 你要放显示abc的标签进去吧;如label
相关知识等待您来回答
编程领域专家
& &SOGOU - 京ICP证050897号javascript - Cannot set property InnerHTML of null - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
This question already has an answer here:
I've a simple html page with no code in the body tag.
I want to insert the html in the body tag through javascript.
My javascript file looks like this.
var Global={
UserWall:function(){
return "a very long html code";
var globalObject=Object.create(Global);
document.getElementsByTagName("body").item(0).innerHTML=globalObject.UserWall();
Now I want this very long html code to be auto inserted in the body tag on page load.
But it is giving me the error I've mentioned in the title.Why?
and also is this the correct way to create ajax based website(no page reloads) meaning that if I called server side script to update this very long html code and appended it to the body of the page.
1,02163060
marked as duplicate by ♦
This question has been asked before and already has an answer. If those answers do not fully address your question, please .
You are almost certainly running your code before the DOM is constructed.
Try running your code in a
handler function (but see note below):
window.onload = function() {
// all of your code goes in here
// it runs after the DOM is built
Another popular cross-browser solution is to put your &script& block just before the closing &/body& tag.
This could be the best solution for you, depending on your needs:
&!-- all of your HTML goes here... --&
// code in this final script element can use all of the DOM
Note that window.onload will wait until all images and subframes have loaded, which might be a long time after the DOM is built.
You could also use document.addEventListener("DOMContentLoaded", function(){...}) to avoid this problem, but this is not supported cross-browser.
The bottom-of-body trick is both cross-browser and runs as soon as the DOM is complete.
Is this running in the &head& of the html? If so, you need to make sure the &body& tag has actually loaded first.
You need to use an onload handler, for example:
I have done all the solutions mentionned here. but they didn't work until i have made this one using Jquery :
in my HTML page :
id="labelid" & &/div&
and when i click on a button, i put this in my JS file :
$("#labelid").html("&label&Salam Alaykom&/label&");
maybe this will work:
document.getElementsByTagName("body")[0].innerHTML
12.1k958109
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledjavascript - cannot set property onclick of null - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
I am goofing around with some plain vanilla JS today and have a question about the onclick event.
Might be best to just show the code:
function _(x) {
return document.querySelector(x);
function toggleSlide(el, maxH) {
var target = _(el);
if(target.style.height == null || target.style.height == maxH+"px") {
target.style.transition = "height 0.3s linear 0s";
target.style.height = "0px";
target.style.transition = "height 0.3s linear 0s";
target.style.height = maxH+"px";
Everything works if I add the onclick event to the button like:
&button id="menu-btn" onclick="toggleSlide('#menu', 280)"&Toggle&/button&
But I get the error,
"connot set property 'onclick' of null"
When I try like this:
_('#menu-btn').onclick = function() {
toggleSlide('#menu', 280)
How can I access my toggle function without adding the onclick in the html?
You are trying to add the listener before the button exists. Either put the problematic code in a separate &script& after the button or in an onload (preferred, see code below).
window.onload = function() {
_("#menu").onclick = function() {
toggleSlide('#menu', 280);
EDIT: Note that if you have multiple of these they should be put in the same onload function, or the last one will override the ones before.
4,08721536
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled当前访客身份:游客 [
Can&t Wait Any Longer~!
:不错,很有用的错误集,果断收藏了!
:引用来自“ht_zhangxd”的评论Hive Editor
:Hive Editor
如果未运行 HiveServer2,应用程...
:楼主总结的很好,bash只是linux上用的比较多,如...
:战略Mark
:总结得够深度,我喜欢,收藏了,谢谢。欢迎继续。
:好,收藏起来。。
今日访问:2
昨日访问:81
本周访问:154
本月访问:200
所有访问:64280
关于JS的各种问题
发表于4年前( 17:57)&&
阅读(1258)&|&评论()
0人收藏此文章,
1.在IE中不支持JS的trim()?
貌似确实如此,那么使用判断吧:if(/^\s*$/.test(‘youtValue’))
引用:.trim() in JavaScript not working in IE
2.如何针对不同的输入框回车事件执行不同的操作?
比如,在一个表单中有一个特殊的输入框,我在这个输入框中输入完东西后只提交这个输入框的值而不提交其它在表单中的输入框的值,这当然用JS很简单的处理就行。但是如果我输入完之后按回车,而不是点击一个button按钮,这样就会提交整个form表单。
我希望的是能够控制这个输入框的回车事件,同时屏蔽掉表单的回车事件(根据这个道理,可以为不同的输入框回车事件执行不同的JS函数)。
首先在form中设置:onkeypress=&if(event.keyCode==13){}&
然后在该input输入框中设置:onkeydown=&if(event.keyCode==13){yourMethod();}&
3.onKeyUp、onKeyDown、onKeyPress这3个事件有什么区别呢(由上题引申)?
onkeypress事件是在按键开始按的时候发生;&
onkeydown事件是在按键已经按下的时候发生;& onkeyup事件是在按键松开(释放)的时候发生。
4.在firefox下面无法使用JS的window.close()方法关闭页面吗?
首先,一定要使用window.showModalDialog()方法打开的页面才能使用window.close()方法关闭的说法是错误的,使用window.open()方法打开的页面同样能被window.close()关闭;
其次,firefox下面一定要进入”about:config“页面设置”dom.allow_scripts_to_close_windows“的值为”true“才能使用window.close()方法关闭页面的说法至少在firefox8.0中文版上是不成立的(其它版本就不知了,^_^)。
5.firefox和chrome不支持window.opener.document.getElementById(&id&)的用法吗?
这个问题用实际效果来表达是,如何接收一个窗口的返回值呢?或者,如何将子窗口的值赋给父窗口的某个属性呢?
同样的代码在IE8和Opera中都可以正常返回,偏偏在chrome14和firefox8中没反应。
window.opener.document.getElementById(&parentID&).value=document.getElementById(&subID&).
打开chrome的调试果然有error(来着console):Uncaught TypeError: Cannot set property 'value' of null
上面的代码有问题吗?我不懂,求高手解答!
6.chrome对select标签的onchange事件支持是一个bug吗?
原因是,我设置了一个size为20的select选择框,然后在select标签中设置了一个onchange(&alert('test');&)事件;但是现在不管里面有没有option(即可选项),chrome都会去执行这个”test“。大惑不解啊!求解答,求高人指点!
更多开发者职位上
1)">1)">1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><li class='page' ng-if="(endIndex<li class='page next' ng-if="(currentPage
相关文章阅读

我要回帖

更多关于 property of null 的文章

 

随机推荐