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

Alb*_*uet 0 html javascript dom

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

我有两个页面(完全相同),我需要禁用一些选择。在其中一个(例如页面 A)上,我使用 getElementById 来检索我的元素,在第二个(例如页面 B)上,我使用 getElement s ById (带有“s”)来检索它(并且它适用于两种情况)。

奇怪的是,如果我在页面 A 上使用 getElement s ById(带有“s”),它会给我错误“document.getElementsById 不是函数”,这是正常的,因为这个函数(带有“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;
                }
            } 
        }
Run Code Online (Sandbox Code Playgroud)

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;
                }
            } 
        }

    }
Run Code Online (Sandbox Code Playgroud)

编辑2:

好的,当它在 B 页(没有 's')上不起作用时,我有

var estAvecGarantie = document.getElementsByName("estAvecGarantie")[0].value;
if(estAvecGarantie){ ... }
Run Code Online (Sandbox Code Playgroud)

我将其替换为

var estAvecGarantie = document.getElementsByName("estAvecGarantie")[0].value;
if(estAvecGarantie == true) { ... }
Run Code Online (Sandbox Code Playgroud)

现在它可以使用不带 's' 的 getElementById

但我仍然不明白为什么它仍然与这个该死的's'一起工作......所以我的问题解决了(ish),但是,如果有人解释为什么我可以使用 getElement s byId() 即使函数不存在(特别是仅在一页上),我洗耳恭听,因为我讨厌我不明白......

hud*_*hab 5

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

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

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

希望能消除您的困惑

  • 我想问题的重点是:“页面 A 上的_getElementsById ...给出...错误...但我在页面 B_ 上没有这个错误”。或者只是我糟糕的英语(或者糟糕的描述?)。 (2认同)
  • @nad 不,请阅读问题,OP 说:“_...如果我在此页面上使用 getElementbyId(不带 's')[B],则它不起作用_”。 (2认同)