Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.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 DOM中不存在add1如何添加?_Javascript_Html - Fatal编程技术网

Javascript DOM中不存在add1如何添加?

Javascript DOM中不存在add1如何添加?,javascript,html,Javascript,Html,我对java脚本和HTML表单有问题 我有一个表单,在表单旁边有一个名为“添加”的按钮,当我单击“添加”时,第二个表单就会出现。表单2旁边是另一个名为add1的按钮,当我单击此按钮时,我希望显示第三个表单。由于某些原因,只有第一个添加按钮起作用 以下是我目前掌握的代码: <style type="text/css"> #newservicesetup1, #newservicesetup2 { display:none; } </style>

我对java脚本和HTML表单有问题

我有一个表单,在表单旁边有一个名为“添加”的按钮,当我单击“添加”时,第二个表单就会出现。表单2旁边是另一个名为add1的按钮,当我单击此按钮时,我希望显示第三个表单。由于某些原因,只有第一个添加按钮起作用

以下是我目前掌握的代码:

<style type="text/css">
    #newservicesetup1, #newservicesetup2 {
        display:none;
    }
</style>

<script type="text/javascript"> 
function showform(theform) { 
    var showHides = new Array('newservicesetup1','newservicesetup2'); 
    for (i=0;i<showHides.length;i++) { 
        document.getElementById(showHides[i]).style.display= 
          (document.getElementById(showHides[i]).id == theform) ? 'block' : 'none'; 
    } 
}

function loadBehaviors () { 
    if (document.getElementById) { 
        document.getElementById('add').onclick = function() { showform('newservicesetup1'); }
        document.getElementById('add1').onclick = function() { showform('newservicesetup2'); }
    } 
} 

window.onload = loadBehaviors;
</script> 

#新闻服务设置1,#新闻服务设置2{
显示:无;
}
函数showform(theform){
var showHides=new数组('newservicestetup1','newservicestetup2');

对于(i=0;i尝试在赋值末尾添加分号:

function loadBehaviors () { 
  if (document.getElementById) { 
    document.getElementById('add').onclick = function() { showform('newservicesetup1'); };
    document.getElementById('add1').onclick = function() { showform('newservicesetup2'); };
  } 
}
您可以使用检查Javascript的语法。

为什么不

document.getElementById('add').onclick = function() {
    document.getElementById('newservicesetup1').style.display = 'block';
}
document.getElementById('add1').onclick = function() {
    document.getElementById('newservicesetup1').style.display = 'block';
}

当您单击相应的按钮时,将显示正确的表单。如果您希望在再次单击时这些表单消失,则只需在loadBehaviors()时稍加修改调用时,DOM中尚不存在add1。将其添加到DOM后绑定到事件处理程序。

是否确定页面上有
add1
元素?另外,请切换到使用数组文字语法,即
var showHides=['newservicesetup1','newservicesetup2']
您永远不必检查文档是否存在。getElementById
:我想不出有javascript引擎的浏览器没有getElementById函数。它只是让您的代码具有不必要的防御性。我该怎么做?