Javascript: getElementById vs getElementsById(两者都在不同的页面上工作)

Javascript: getElementById vs getElementsById (both works on different pages)

本文关键字:工作 getElementById vs getElementsById 两者都 Javascript      更新时间:2023-09-26

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

我有两个页面(完全相同),我需要禁用一些选择。在其中一个页面(比如页面A)上,我使用getElementById来检索我的元素,在第二个页面(比如页面B)上,我使用getElementsById(带's')来检索它(它在两种情况下都有效)。

奇怪的是,如果我在页A上使用getElementsById(带有's'),它会给我错误的"文档"。getElementsById不是一个函数",这很正常,因为这个函数(带's')通常不存在。但是我在B页没有这个错误,如果我在本页上使用getElementById(没有's'),它工作!

有人能给我一个解释吗?(如果这样下去,我头上剩下的几根头发都要掉光了……)

提前感谢!

Ps:对不起,我的英语很差!

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

页面:

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上工作时(没有's'),我有

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

我用

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

现在它使用不带's'的getElementById工作

但是我还是不明白为什么这个该死的"s"还在工作…所以我的问题解决了(ish),但仍然,如果有人解释为什么我可以使用getElementsbyId()即使函数不存在(特别是只在一个页面上),我都在听,因为我讨厌当我不理解…

正如James在这里所描述的,id值在一个文档中必须是唯一的,所以只有一个"元素"匹配,而不是多个"元素"。

这就是为什么在选择元素时不应该使用s。因为每次只能选择一个Id

然而,也有返回多个元素的方法,这些方法确实使用复数形式的"元素",如getElementsByTagName

希望这能消除你的困惑

重要的事情先说:JavaScript中的函数名,或者说方法名是区分大小写的。这意味着文档。getElementById与document.getElementbyId不一样。

奇怪的部分:文档。getElementsById在JavaScript中不存在,所以默认情况下它不能工作。这可以工作的唯一方法是,如果有人在另一个页面上创建了这个函数/方法。更明显的解释是你在第二页上打了个0字。也许你忘了写S,你以为你没有。你能再试一次吗?