首页 > 给一个按钮设置事件,出了个问题

给一个按钮设置事件,出了个问题

<div id="page">
   
    <input type="button" id ="button1″ value="1″ onclick= "x(event)" />
    <input type="button" id ="button2″ value="2″ />
</div>

下面是JavaScript部分:

function x(e){
var t=e.target//提示缺少函数??
var parent =t.parentNode;
 parent.removeChild(t);
  parent.appendChild(t);
  }

一楼说得很正确,此时的this是window对象


你的this出问题了,你用的this是window对象。你应该在绑定元素里面加入参数e,然后用e.target才是此事件产生的对象。


一楼的答案是正确的啊,我试过了。大神!!

【热门文章】
【热门文章】