Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.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/9/spring-boot/5.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_Css - Fatal编程技术网

我的javascript函数无法运行所有代码

我的javascript函数无法运行所有代码,javascript,css,Javascript,Css,请帮助我解决它,如果您喜欢这样写: function openNav() { document.getElementById("mySidenav").style.width = "250px"; document.getElementById("main").style.marginLeft = "250px"; document.getElementById("name-page").style.width = "738px"; document.getEle

请帮助我解决它,如果您喜欢这样写:

function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.getElementById("name-page").style.width = "738px";
    document.getElementById("news").style.marginLeft = "0";
    document.getElementById("ida").style.marginLeft = "0";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
    document.getElementById("name-page").style.width = "930px";
    document.getElementById("news").style.marginLeft = "125px";
    document.getElementById("ida").style.marginLeft = "125px";
}
样式
id=news
可以更改,但样式
id=ida
不能更改 如果你喜欢这样写:

function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    document.getElementById("main").style.marginLeft = "250px";
    document.getElementById("name-page").style.width = "738px";
    document.getElementById("ida").style.marginLeft = "0";
    document.getElementById("news").style.marginLeft = "0";
}


function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "0";
    document.getElementById("name-page").style.width = "930px";
    document.getElementById("ida").style.marginLeft = "125px";
    document.getElementById("news").style.marginLeft = "125px";
}
样式
id=news
铁路超高变化,但样式
id=ida
变化


请帮助我,对不起,我的语言是

在设置属性之前检查元素是否存在:

...
if(document.getElementById("news")) {
    document.getElementById("news").style.marginLeft = "0";
}    
if(document.getElementById("ida")) {
    document.getElementById("ida").style.marginLeft = "0";
}
...

你能显示相应的html吗?我不会点击这些链接,但很明显你在两个html页面上使用了相同的javascript文件。您应该考虑使用变量(<代码> var IDAeleMe= Deal.GETelEnMyByID(“IDA”);< /COD>),并在尝试访问<代码>样式>边距< /代码>之前检查它是否为空/未定义。