最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

events - Can you use javascript to modify onmousedown of div at runtime - Stack Overflow

programmeradmin1浏览0评论

I want to write some JavaScript that will change the onmousedown of a div at runtime. So on load a mouse down on the div will do one thing and if a JavaScript function is called a mouse down on the div will do something else. Is this possible?

I want to write some JavaScript that will change the onmousedown of a div at runtime. So on load a mouse down on the div will do one thing and if a JavaScript function is called a mouse down on the div will do something else. Is this possible?

Share Improve this question edited Dec 6, 2013 at 14:50 BenMorel 36.6k51 gold badges205 silver badges335 bronze badges asked Oct 5, 2008 at 0:36 mrjrdnthmsmrjrdnthms 1,6294 gold badges25 silver badges35 bronze badges 1
  • @ mrjrdnthms : What Prestaul is attaching to the handler is the definition of a new function. What you are attaching is the return of the alert function, that is void/undefined, IIRC. This is why is code works, and your won't. – paercebal Commented Oct 5, 2008 at 12:04
Add a ment  | 

1 Answer 1

Reset to default 13

You can just assign the onMouseDown property.

document.getElementById('myDiv').onmousedown = function() {
  alert('New mouse down handler.');
};

Keep in mind that javascript properties are case-sensitive. "onmousedown" is all lower-case.

发布评论

评论列表(0)

  1. 暂无评论