我試圖用一個(gè)非常小的js代碼隱藏多個(gè)元素。如果我只包含一個(gè)類,它工作得很好,但是如果我包含多個(gè)類,那么它就不再工作了。具體而言,只有插入的第一個(gè)選擇器始終隱藏。有人能告訴我我做錯(cuò)了什么嗎?感謝您的幫助,謝謝您的回復(fù)。
這就是我的工作
window.onload = function() {
if (window.location.href.includes('?lost_pass=1')) {
//Hide the element.
document.querySelectorAll('.hide_login_title')[0].style.display = 'none';
document.querySelectorAll('.hide_login_msg')[0].style.display = 'none';
}
};
這就是我想做的
window.onload = function() {
if (window.location.href.includes('?lost_pass=1')) {
//Hide the element.
document.querySelectorAll('.hide_login_title, .hide_login_msg')[0].style.display = 'none';
}
};
看起來您需要遍歷querySelectorAll返回的元素。有兩種方法可供選擇,一種是節(jié)點(diǎn)列表的built-in
.forEach()
方法。它看起來像這樣: