Javascript:getElementById vs getElementsById(两者都适用于不同的页面)

时间:2016-09-07 10:15:23

标签: javascript html dom

我正在努力解决一个非常奇怪的问题...

我有两个页面(完全相同),我需要禁用一些选择。在其中一个(比如第A页)中,我使用getElementById来检索我的元素,而在第二个(比如第B页)我使用getElement s ById(使用' s&#39 ;)检索它(它适用于两种情况)。

奇怪的是,如果我在页面A上使用getElement s ById(使用''),它会给我错误" document.getElementsById is不是一个功能",这是正常的,因为这个功能(' s')通常不存在。但是我在第B页上没有出现此错误,如果我在此页面上使用了getElementById(没有' s'),那么不会有效! ?!?

有人能给我一个解释吗? (如果继续......我会失去头上留下的几根头发......)

提前致谢!

Ps:抱歉我的英语很差!

编辑:这是我的网页代码:

页面A:

function controleDelaiFranchise (casChoix){
        var estAvecGarantie = <bean:write property="avecGarantie" name="simulationAutonomeForm" filter="false"/>;

        if(estAvecGarantie ==true){

            if(casChoix == 'Emprunteur'){
                document.getElementById("assDelaiFranchiseEmpr").disabled = false;
            }
            else {
                if(casChoix == 'CoEmprunteur'){
                    document.getElementById("assDelaiFranchiseCoEmpr").disabled = false;
                }
            } 
        }
        else{

            if(casChoix == 'Emprunteur'){
                document.getElementsById("assDelaiFranchiseEmpr").disabled = true;
            }
            else {
                if(casChoix == 'CoEmprunteur'){
                    document.getElementById("assDelaiFranchiseCoEmpr").disabled = true;
                }
            } 
        }

第B页:

function controleDelaiFranchise (casChoix){
        var estAvecGarantie = document.getElementsByName("estAvecGarantie")[0].value;

        if(estAvecGarantie){

            if(casChoix == 'Emprunteur'){
                document.getElementsById("assDelaiFranchiseEmpr").disabled = false;
            }
            else {
                if(casChoix == 'CoEmprunteur'){
                    document.getElementsById("assDelaiFranchiseCoEmpr").disabled = false;
                }
            } 
        } else {

            if(casChoix == 'Emprunteur'){
                document.getElementsById("assDelaiFranchiseEmpr").disabled = true;
            }
            else {
                if(casChoix == 'CoEmprunteur'){
                    document.getElementsById("assDelaiFranchiseCoEmpr").disabled = true;
                }
            } 
        }

    }

编辑2:

好的,当它没有在B页上工作时(没有&#39; s&#39;)我有

var estAvecGarantie = document.getElementsByName("estAvecGarantie")[0].value;
if(estAvecGarantie){ ... }

我用

替换它
var estAvecGarantie = document.getElementsByName("estAvecGarantie")[0].value;
if(estAvecGarantie == true) { ... }

现在它可以使用getElementById而不使用&#39;

但是我仍然不明白为什么它仍然在使用这个该死的&#39;&#39; ...所以我的问题已经解决了(ish),但是,如果有人解释为什么我可以使用getElement s byId(),即使函数不存在(具体而言)只在一页上,我全都耳朵,因为我讨厌当我不明白......

2 个答案:

答案 0 :(得分:3)

James here 所述,id值必须在文档中唯一,因此只有一个“元素”匹配,而不是多个“元素”。

这就是原因,我们不应该在选择元素时使用 s 。由于Id一次只能选择一个。

但是,有些方法会返回多个使用复数“元素”的元素,例如getElementsByTagName

希望能清除你的困惑

答案 1 :(得分:2)

首先要做的事情: 函数 - 或者更确切地说,JavaScript中的方法名称是区分大小写的。这意味着document.getElementById与document.getElementbyId不同。

奇怪的部分: document.getElementsById在JavaScript中不存在,因此默认情况下无法正常工作。唯一可行的方法是,如果有人在另一页上创建了这个函数/方法。更明显的解释是你在第二页上做了一个类型o。也许你忘了写S而你以为你没有。你能再试一次吗?