2011-08-10 26 views

回答

1

使用event.target检查已点击的元素的类型 -

$('table input, table select').focus(function(event){ 
if ($(event.target).attr("type") === 'text') { 
} 
}) 

http://jsfiddle.net/ipr101/vJPr3/

编辑

更清晰的例子,而无需使用事件。目标 -

$('input, select').focus(function(){ 
    if($(this)[0].tagName === 'SELECT') alert('select'); 
    if($(this).attr("type") === 'text') alert('text');  
}) 

http://jsfiddle.net/ipr101/vJPr3/1/

+0

...非常酷,但是,是什么在$(本),在这种情况下是为$(event.target)的优势在哪里? – vector

+0

...当我提醒'选择'元素的类型属性我得到'选择一',所以这里是我的检查:if($(this).attr(“type”)==='select-one') – vector

+0

这是一个很好的观点 - $(this)在你的例子中也可以工作。我会更新答案。 – ipr101