我是谁或我如何知道我正在处理的元素?

Abd*_*UTI 3 javascript jquery

嘿,看看这段代码:

$("li,p").click(function(){

   // how do I perform a test, and know wich Element it is ?

   if( /* I'm a <li/> */ )
       // Do this for the <li/>

   if( /* I'm a <p/> */ )
       // Do this for the <p/>
})
Run Code Online (Sandbox Code Playgroud)

谢谢你们^^

Sau*_*aul 6

一种选择是使用is()方法:

$("li,p").click(function(){

   // how do I do I perform a test, on wich Element it is ?

   if($(this).is('li'))
       // Do this for the <li/>

   if($(this).is('p'))
       // Do this for the <p/>
})
Run Code Online (Sandbox Code Playgroud)