获取标签名称

use*_*316 1 html javascript jquery

让我们说我有HTML:

<input name="something" id="someid" />
Run Code Online (Sandbox Code Playgroud)

然后在Jquery中,我想点击一些元素,然后点击后我想得到元素的标签名称.

所以this.getTheTagName.

到目前为止我尝试的所有东西都不起作用,特别是对于输入标签.

Thi*_*ter 7

您可能意味着name 属性而不是标记名称.

alert($('#someid').attr('name'));
Run Code Online (Sandbox Code Playgroud)

如果你的意思是标签名称,请使用.prop('tagName'):

alert($('#someid').prop('tagName'));
Run Code Online (Sandbox Code Playgroud)

这是两个例子:http://jsfiddle.net/wwQ7h/


Cfr*_*eak 7

$('#someid').click( function() {
    alert( this.tagName );
});
Run Code Online (Sandbox Code Playgroud)

某些浏览器可能无法在输入时执行click事件.相反,你可能想要focus()

$('#someid').focus( function() {
     alert( this.tagName);
});
Run Code Online (Sandbox Code Playgroud)