Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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 CSS未捕获类型错误:无法读取属性';setAttribute';空的_Javascript_Jquery_Css - Fatal编程技术网

Javascript CSS未捕获类型错误:无法读取属性';setAttribute';空的

Javascript CSS未捕获类型错误:无法读取属性';setAttribute';空的,javascript,jquery,css,Javascript,Jquery,Css,我试图给利润率最高:10px的id下载csv 以下是html和javascrpit: <span style="display: inline;" id="download-csv"><input type="submit" id="csv_download" value="Download"></span>; JavaScript : var download_csv=document.getElementById("down

我试图给利润率最高:10px的id下载csv

以下是html和javascrpit:

<span style="display: inline;"  id="download-csv"><input type="submit"   id="csv_download"  value="Download"></span>;



JavaScript :
         var download_csv=document.getElementById("download-csv");
if(count_layer==1)
        download_csv.setAttribute("style","margin-top:10px");
}else{
       download_csv.setAttribute("style","margin-top:20px");

}
;
JavaScript:
var download_csv=document.getElementById(“下载csv”);
如果(计数层==1)
下载_csv.setAttribute(“样式”,“页边距顶部:10px”);
}否则{
下载_csv.setAttribute(“样式”,“页边距顶部:20px”);
}
它抛出错误
类型错误:无法读取null属性“setAttribute”

您需要确保在JS代码访问这些元素之前加载标记

此外,不能为
内联
元素指定
边距
s,请将
显示
样式属性设置为
显示内联

var download_csv=document.getElementById(“下载csv”);
var count_layer=1;
如果(计数层==1){
下载_csv.style.marginTop=“10px”;
}否则{
下载_csv.style.marginTop=“50px”;
}

您可以尝试以下代码:

if(count_layer==1)
        download_csv.style.marginTop = "10px";
}else{
       download_csv.style.marginTop = "20px";
}

现在,您的javascript代码在页面加载之前执行,因此
属性尚未加载,然后您将得到错误

要修复它,请将javascript代码放入$(document).ready中


很抱歉编写了双重时间样式。样式这是唯一的样式。请重试此样式。先生,我编写了此样式,但下载时出现相同错误。\u csv.style.marginTop=“10px”;谢谢,先生,我不确定这家伙是否在使用jqueryMostly JavaScript
$(document).ready(function() {
    //Your code goes here
}) ;