Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取空问题的属性样式_Javascript_Properties_Null - Fatal编程技术网

Javascript 无法读取空问题的属性样式

Javascript 无法读取空问题的属性样式,javascript,properties,null,Javascript,Properties,Null,我已经做了这个函数,它给出了错误 无法读取null的属性“style” 你能帮我找出问题所在吗 function cade() { var a = document.getElementById(this.id).style.top; a = a + 100; document.getElementById(this.id).style.top = a+'px'; } cade.call(document.getElementById("p")); 当您的功能正常工作

我已经做了这个函数,它给出了错误

无法读取null的属性“style”

你能帮我找出问题所在吗

function cade()
{
    var a = document.getElementById(this.id).style.top;
    a = a + 100;
    document.getElementById(this.id).style.top =  a+'px';
}
cade.call(document.getElementById("p"));

当您的功能正常工作时,该元素上的样式会阻止您做很多事情来移动它。添加
position:absolute
或沿这些线添加的内容将使其正确移动

由于您将元素作为
this
传递,因此不需要使用
getElementById
再次获取它。您拥有的引用已经完全有效,因此您可以简化代码,如下所示:

函数cade(){
var a=this.style.top;
a=a+100;
this.style.top=a+'px';
}
cade.call(document.getElementById(“p”))
div#p{
位置:绝对位置;
}

这是一些文本。
document.getElementById(This.id)返回Null我有带“p”id的div。为什么这可能无效?在函数中,
console.log(This)
返回什么?如果
null
(看起来很可能是这样),您可能需要阅读这个问题及其公认的答案:您是否可以包括相关的HTML和任何可能适用于它的CSS?我怀疑这与缺少样式规则有关,导致未应用
top
,而不是ID或执行顺序出现问题。