参考错误未定义
reference error not defined
create2.innerHTML = create2.innerHTML+ " <img onclick = 'a()' src =' "+ messages[msg].thumb + "' height='90' width='142'> </a> ";
function a (){
}
So I am trying to include an onlick event on image tag so that when it is click a function is called. However, I am getting the following error :
Uncaught ReferenceError: a is not defined onclick
有人可以解释为什么会这样吗?
这就是为什么使用内联事件不是一个好主意。您的范围是 window,因此它会查找 window 中的点击。当函数在另一个函数中时,您无权访问它。
var img = document.createElement("img");
img.src = messages[msg].thumb;
img.style.height = "90px";
img.style.width = "142px";
img.onclick = a; //would be better to use addEventListener()
create2.appendChild(img);
create2.innerHTML = create2.innerHTML+ " <img onclick = 'a()' src =' "+ messages[msg].thumb + "' height='90' width='142'> </a> ";
function a (){
}
So I am trying to include an onlick event on image tag so that when it is click a function is called. However, I am getting the following error :
Uncaught ReferenceError: a is not defined onclick 有人可以解释为什么会这样吗?
这就是为什么使用内联事件不是一个好主意。您的范围是 window,因此它会查找 window 中的点击。当函数在另一个函数中时,您无权访问它。
var img = document.createElement("img");
img.src = messages[msg].thumb;
img.style.height = "90px";
img.style.width = "142px";
img.onclick = a; //would be better to use addEventListener()
create2.appendChild(img);