TypeError: null不是对象(计算'document.getElementById(x).style&
TypeError: null is not an object (evaluating 'document.getElementById(x).style')
我想让我的javascript做什么:
我有一个div,其中包含一个"onmouseover='getPosition(x)'"是添加的,也可以在我的javascript中删除,当我按下某个按钮。函数'getPosition()总是在div尚未删除时运行,以便它总是获取和更新div的位置,并在文本字段占位符
中显示它的x和y位置。$("#canvas").append('<div onmouseover="getPosition(this.id);"style="top:'+initXYpos+'px; left:'+initXYpos+'px;">Button '+index+'</div>');
问题:
当我删除包含"getPosition(this.id)"的div时,它仍然运行该函数,同时给出一个错误:"TypeError: null不是对象(求值'document.getElementById(x).style')"
我只是想让它停止运行当我删除div。
getPosition(x):
function getPosition(x){
$('#canvas_container').mouseover(function(){
timer = setInterval(function() {
var xPos = document.getElementById(x).style.left;
var yPos = document.getElementById(x).style.top;
document.getElementById(x+'_fieldX').placeholder = xPos;
document.getElementById(x+'_fieldY').placeholder = yPos;
}, 10); }
注意:
如果你认为我的问题是由一个愚蠢的错误引起的,我很抱歉。我对Javascript真的很陌生,希望你能理解并乐于帮助我。
在您的javascript中,当您删除div时,您还必须使用以下代码行停止计时器:
clearInterval(timer);
我的建议是删除计时器,因为你并不真的需要它来实现你想要做的事情
相关文章:
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 如何用更合适的内容替换document.write
- document.styleSheets不返回任何内容
- jQuery document.ready not working
- $(document).height()在刷新时随机化值(Safari 5.1.10)
- reactjs this.refs vs document.getElementById
- 如何通过adf中的document.getElementById获取inputText字段值
- window.opener.document在ie中不起作用
- contentWindow.document.body is null
- document.applet.method在Mounatin Lion上抛出safari 6+JDK7异常
- document.getElementById(“st”).click();不起作用
- $(document).height和$(window).heaght都返回相同的值-使用的是正确的doctype
- jQuery document.ready停止代码
- document.getElementById并使用id名称
- 使用jQuery 1.8.1准备好多个$(document).是否有任何开销
- document.getElementById在js中不起作用
- document.getElementById.style.backgroundImage not working
- 停止对document.ready函数的重定向/刷新
- 是否可以 document.getElementsByTagName('head')[0] ever 返回 null