简体   繁体   中英

how do I get access to javascript event object inside tag

How do I access the onTouchstart event from the html attribute? Is there a way of doing this?

<img src="img/ui/arrow-left.svg" ontouchstart="e.preventDefault(); /*my code here*/ ">

I'd prefer not to have to do something like

el.addEventListener("touchstart", handleStart, false);

function handleStart(evt) {
   evt.preventDefault();
   //my code here
}

Unfortunately for you, your preferred method of inline event handling is considered poor practice. You and your application would be much better off with abstracted event handling.

Inline event handling reduces maintainability and readability. However, it does not reduce performance either way you go. If you pulled up a website to others and it uses inline scripting, there will be instant critisism of your work. Not to mention (from the programmers answer on this (cannot paste links for some reason)) that HTML and CSS files should only contain that -- all javascript is better put in a seperate file to abstract the view from the controller. Readability and maintainability are more important that having a script / site that simply works.

Since event is not defined as a callback parameter, it defaults to event not e .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM