2016-05-27 5 views

答えて

2

前記引数

function Myfunction(elem) { 
 
    var x = elem.id; //Or this.getAttribute('id') 
 
    console.log(x) 
 
}
<div id="01" onclick="Myfunction(this)">Click me</div>
thisとして(現在の要素)を通過上

0

はい、それは以下のこの上のデモです...可能です。..

<!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を忘れないでください... divが名前にundefinedを返します理由です..

関連する問題