私は最初の純粋なJavaScriptアプリケーションで作業していますが、私は以下のコードの最後のブロックにある私のdocument.getElementById
が機能していない理由を理解できません。私はdocument.addEventListener('DOMContentLoaded', function() {first functions called}
でスクリプトを開始し、要素は問題なくDOMにロードされています。ここでdocument.getElementByIdが動作しないのはなぜですか?
要素がマイページに追加される前に、私のコードが最後のブロック(先頭var x
)にジャンプしているという問題がありますか?もしそうなら、コードをハングアップするよう指示する方法はありますか?最終的には、document.getElementById
を後で(DOMを変更するために)呼び出すことにします。
function populateRefineMenu(arr, chosenArr, category) {
var categoryUL = document.createElement('ul');
categoryUL.className = 'refine-menu-ul';
categoryUL.Id = 'refine-' + category;
var clearFilter = document.createElement('li');
clearFilter.className = 'clear-filter';
clearFilter.Id = 'genre-clear-filter';
console.log('This ID is ' + clearFilter.Id); // logs fine
clearFilter.innerHTML = 'Clear Filter';
console.log("clearFilter is " + clearFilter.innerHTML); // logs fine
refineMenu.appendChild(categoryUL);
categoryUL.appendChild(clearFilter);
var x = document.getElementById('genre-clear-filter');
console.log(x) // logs "null"
console.log(x.innerHTML) // "cannot read property 'innerHTML'
}
ありがとうございました!
オブジェクトのプロパティは、大文字と小文字が区別されています。それは 'clearFilter.id'でなければなりません。 – JJJ
ああ、私はidを設定するのではなく、オブジェクトの新しいキーと値を作成していました。とった。ありがとう! – dedaumiersmith