2016-05-27 1 views

Antwort

2

Verwenden this.id und this (aktuelles Element) als Argument

function Myfunction(elem) { 
 
    var x = elem.id; //Or this.getAttribute('id') 
 
    console.log(x) 
 
}
<div id="01" onclick="Myfunction(this)">Click me</div>
passieren

0

Ja, es ist möglich ... unten Demo auf diesem ist ..

<!DOCTYPE html> 
<html> 
    <body> 
     <script> 
     function Myfunction(ctrl){  
      var id= ctrl.id; 
      var name= ctrl.name;  
      console.log(id); 
      console.log(name); 
      alert(id); 
      alert(name); 
     } 
     </script> 
     <div id="01" name='divOne' onclick="Myfunction(this)">div sample</div> 
     <input id="02" type="number" name='txtOne' onclick="Myfunction(this)"></p>  
    </body> 
</html> 

only input types html controls can have name property Denken Sie daran, .. das ist, warum divundefined auf Namen zurück ..

Verwandte Themen