[英]Invoking event handler doesn't seem to work
我有可折疊物品的代碼。
var coll = document.getElementsByClassName("collapsible");
for (var i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var p = this.nextElementSibling;
if (p.style.maxHeight){
p.style.maxHeight = null;
} else {
p.style.maxHeight = p.scrollHeight + "px";
}
});
if (window.screen.availWidth >= 768) coll[i].onclick.apply(coll[i]);
}
最后一行是為了在更大的屏幕上自動展開所有可折疊項。 但它給了我一個錯誤:
Uncaught TypeError: Cannot read property 'apply' of null
我怎樣才能解決這個問題?
coll[i].onclick is null, onclick is an event not a function, so you would like to use click function there.
不知道你想通過傳遞 coll[i] 來實現點擊 function。 但嘗試:
coll[i].click.apply()
或更簡單:
coll[i].click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.