[英]Microsoft JScript runtime error: Unable to get value of the property 'radalert': object is null or undefined IE8
[英]Unable to get value of the property 'call': object is null or undefined IE8
在IE8中,我的菜單腳本似乎運行不正常。 但是經過大量搜索之后,我無法弄清為什么會引發如下錯誤:
SCRIPT5007: Unable to get value of the property 'call': object is null or undefined
site.js, line 5 character 5
我的腳本是:
var anchor = document.querySelectorAll('button');
[].forEach.call(anchor, function(anchor){
var open = false;
anchor.onclick = function(event){
event.preventDefault();
if(!open){
this.classList.add('close');
open = true;
}
else{
this.classList.remove('close');
open = false;
}
};
});
該腳本處理了我的移動導航,但是由於我仍在學習中,因此我無法弄清楚這一點。 誰能幫助我指出正確的方向?
IE8不支持Array.forEach() 。
由於您已使用jQuery標記了問題,因此請使用$ .each()迭代數組。
使用jQuery可以寫成
$('button').click(function(){
$(this).toggleClass('close')
})
一個vanila腳本解決方案可以是
var anchor = document.querySelectorAll('button');
function handler(e) {
if (/\bclose\b/.test(this.className)) {
this.className = this.className.replace(/\bclose\b/, '');
} else {
this.className += ' close';
}
}
for (var i = 0; i < anchor.length; i++) {
if (window.addEventListener) {
anchor[i].addEventListener('click', handler, false);
} else {
anchor[i].attachEvent('onclick', handler);
}
}
演示: 小提琴
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.