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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39367126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:31:00  来源:igfitidea点击:

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

javascripthtmldom

提问by Alban Becquet

I'm struggling with a really weird problem...

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

I have two pages (quite the sames) where I need to disable some selects. On one of them (say page A), I use getElementById to retrieve my element, and on the second one (say page B) I use getElementsById (with a 's') to retrieve it (and it works on both cases).

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

What is weird is that if I use getElementsById on page A (with the 's'), it gives me the error "document.getElementsById is not a function", which is normal because this function (with the 's') normally doesn't exist. But I don't have this error on page B, and if I use getElementById (without the 's') on this page, it doesn'tworks !?!?

奇怪的是,如果我在页面 A 上使用 getElement sById(带有 's'),它会给我错误“document.getElementsById 不是函数”,这是正常的,因为这个函数(带有 's')通常不存在。但是我在页面 B 上没有这个错误,如果我在这个页面上使用 getElementById(没有 's'),它就不起作用!?!?

Can someone give me an explanation ? (I'll lose the few hairs left on my head if it continue ...)

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

Thanks in advance!

提前致谢!

Ps: Sorry for my poor english!

Ps:对不起,我的英语不好!

Edit: Here is the code of my pages:

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

Page A:

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

Page B:

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

    }

Edit 2:

编辑2:

Ok so when it was not working on page B (without 's') I had

好的,所以当它不在页面 B 上工作时(没有 's')我有

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

I replace it with

我用它代替

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

and now it works using getElementById without the 's'

现在它可以使用没有“s”的 getElementById

But I still don't understand why it's still working with this damn 's' ... So my problem is solved (ish), but still, if someone have an explanation for why can I used getElementsbyId() even if the function doesn't exist (and specifically on one page only), I'm all ears because I hate when I don't understand ...

但我仍然不明白为什么它仍然与这个该死的 's' 一起工作......所以我的问题解决了(ish),但是,如果有人解释为什么我可以使用 getElement sbyId() 即使功能不存在(特别是仅在一页上),我全神贯注,因为我讨厌当我不明白时......

回答by hud

As described by Jameshereid values have to be unique in a document, so there will be only one "element" that matches, rather than multiple "elements".

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

That is the reason, We should not use swhile selecting elements. As Idcan be selected only one at a time.

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

However, there are methods that return multiple elements which do use the plural "elements", such as getElementsByTagName.

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

Hope that clears your confusion

希望能解决你的困惑

回答by Marc van Nieuwenhuijzen

First things first:Function-, or rather, methodnames in JavaScript are Case-Sensitive. This means that document.getElementById is not the same as document.getElementbyId.

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

The weird part:document.getElementsById does not exsist in JavaScript, so it can't work by default. The only way this can work is if somebody created this function/method on the other page. A more obvious explanation is that you made a type-o on your second page. Maybe you forgot to write the S and you thought you didn't. Can you try again?

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