日期:2014-05-16 浏览次数:20477 次
function init() { var el = document.getElementById('MyElement'); el.onclick = function () {……} } init();
<html> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> </HEAD> <body> <div> <input type="button" value="aaaa"> <br> <input type="button" value="bbbb" id="MyElement"> </div> <SCRIPT type="text/javascript"> function init() { var el = document.getElementById('MyElement'); el.onclick = function () {alert('haha');}; } init(); </SCRIPT> </body> </html>
------解决方案--------------------
function init() { var el = document.getElementById('MyElement'); var a=1; var tmp=function(){alert(a)}; el.onclick = tmp; } init();
------解决方案--------------------