2011-08-10 21 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