我该怎么把事件放在onclick上?

时间:2016-04-02 05:56:13

标签: javascript

<img src="image1.png" onclick="handler(this.src)" />

function handler(event, handler_src){
  if(onClickOn){
    companyJes(handler_src);
  }else{
    event.preventDefault(); // don't do anything
    event.stopPrpagation(); // really don't do anything

    return false; //make sure you don't do anything
  }
}

on onclick handler(what should i put this ?, this.src);我应该在onclick上添加变量来替换事件?

我测试了handler(event, this.src);,但它确实有效。

1 个答案:

答案 0 :(得分:0)

您需要在处理程序中传递事件对象。 E.g:

handler(event)

因为“this”将调用处理函数的HTML元素

请参阅JSFiddle