Javascript:getElementById与getElementsById(两者均适用于不同的页面) [英] Javascript: getElementById vs getElementsById (both works on different pages)

查看:60
本文介绍了Javascript:getElementById与getElementsById(两者均适用于不同的页面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个非常奇怪的问题而苦苦挣扎...

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

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

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页上使用getElement s ById(带有's'),则会出现错误"document.getElementsById不是函数",这是正常现象因为此功能(带有"s")通常不存在.但是我在B页上没有此错误,并且如果我在此页上使用getElementById(不带'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't works !?!?

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

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

提前谢谢!

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

Ps: Sorry for my poor english!

这是我页面的代码:

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

    }

好吧,当它不在B页上​​工作时(没有's')

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

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

我替换为

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

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

and now it works using getElementById without the 's'

但是我仍然不明白为什么它仍然可以与这个该死的's'一起工作...所以我的问题解决了(ish),但是,仍然有人对我为什么可以使用getElement s做出了解释 byId(),即使该功能不存在(并且仅在一页上),我也听不清,因为我讨厌在不了解的情况下...

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 ...

推荐答案

James

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

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

That is the reason, We should not use s while selecting elements. As Id can be selected only one at a time.

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

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

希望能消除您的困惑

这篇关于Javascript:getElementById与getElementsById(两者均适用于不同的页面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆