0

ページ上のすべての selectBoxes の値をリセットしようとしています。問題のイベントは onClick で発生しているため、すべてが DOM に読み込まれます。

selectBoxes の HTMLCollection を取得しましたvar selectBoxes = document.getElementsByClassName("selectBox");。これらの selectBoxes のそれぞれの selectIndex を 0 に変更したいと思います。

ただし、HTMLCollection から特定のオブジェクトのプロパティにアクセスしようとするたびに、未定義の値になってしまいます。

selectBoxes.item(0).selectedIndex = 未定義
selectBoxes[0].selectedIndex = 未定義

var selectBoxes = document.getElementsByClassName("filterBox");
  console.log(selectBoxes); //a nice JavaScriptObject
  for(i = 0; i < selectBoxes.length; i++){
      selectBoxes.item(i).selectedIndex = 0;
  }

これは HTMLCollections に関する非常に興味深い記事です: https://hackernoon.com/htmlcollection-nodelist-and-array-of-objects-da42737181f9これは、私の問題の一部を説明しているようです。

4

1 に答える 1

0

selectedIndex プロパティに間違ってアクセスしていました。それはselectBox自体の直接のプロパティではなく、selectBoxの子の中にあるようです。

  var selectBoxes = document.getElementsByClassName("filterBox");
  for(i = 0; i < selectBoxes.length; i++){
      selectBoxes.item(i).children[0].selectedIndex = 0;
  }
于 2018-02-03T20:06:18.353 に答える