Js gets the event source and the object that triggered the event

  • 2020-03-26 21:38:33
  • OfStack

An Html element has an onclick method: onclick='return myfunction(event)'
 
function myfunction(event) { 
event = event ? event : window.event; 
var obj = event.srcElement ? event.srcElement : event.target; 
//Obj is the object that triggers the event, and you can use its properties
//You can also convert obj to a jquery object for other elements
var $obj = $(obj); 
alert($obj.parent().attr("href")); 
} 

Related articles: