1. ホーム
  2. javascript

[解決済み】Javascript:getElementById対getElementsById(両方が別のページで動作する)。

2022-01-19 09:28:02

質問

本当に奇妙な問題で悩んでいるんだ...。

私は、いくつかの選択を無効にする必要がある2つのページ(全く同じもの)があります。そのうちの1つ (ページ A) では、要素を取得するために getElementById を使用し、2つ目のページ (ページ B) では getElement を使用します。 s ById('s'が付いている)で取得する(そしてそれは両方のケースで動作する)。

変なのは、getElementを使うと s ページA('s'付き)のByIdでは、エラー "document.getElementsById is not a function" が出ますが、この関数('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ページ('s'なし)で動作していないときは、次のようにしました。

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

で置き換えています。

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

そして、's' を除いた getElementById を使用して動作するようになりました。

しかし、私はまだこのいまいましい 's' で動作している理由を理解していない... ... というわけで、私の問題は解決したのですが、それでも、もし誰かが、なぜ私がgetElement s byId() 関数が存在しなくても(特に1ページだけ)、私は理解できないときが嫌いなので、私はすべての耳を傾ける...

どのように解決するのですか?

の説明の通りです。 James こちら id 値はドキュメント内で一意でなければならないので、複数の "element" ではなく、マッチする "element" が1つだけ存在することになります。

そのため s を選択することができます。というように Id は一度に1つしか選択できません。

ただし、複数の要素を返すメソッドの中には、以下のように複数形の "elements"を使用するものがあります。 getElementsByTagName .

あなたの混乱を解消することを願っています。