在html事件属性中调js函数, this问题

JavaScript015

在html事件属性中调js函数, this问题,第1张

this 在dom里写 onclick="demo()" 和 document.getElementById("bButton").onclick = demo

意义不一样。

你alert一下this就知道了,A触发的this是demo这个方法本身。B触发的this才是B

写在dom里的方法其实相当于

document.getElementById("aButton").onclick = function(){ demo()}

将下面这行

document.write ("<img src='Plus.PNG' style='cursor:hand' onclick='Show("+Type+")' >")

改为

document.write('<img src="Plus.PNG" style="cursor:hand" onclick="Show(\''+Type+'\')" >')

因为你要确保生成的调用代码是

Show('qasd')

而不是

Show(qasd)

前者是字符串,后则则被解释为变量名.