Get the tag name
Let us say I have in HTML:
<input name="something" id="someid" />
And then in Jquery, I want to click on some element and开发者_开发技巧 after I click I want to get the tag name of the element.
So this.getTheTagName.
Everything I tried so far does not really work, especially for input tag.
You probably mean the name
attribute and not the tag name.
alert($('#someid').attr('name'));
If you did mean the tag name, use .prop('tagName')
:
alert($('#someid').prop('tagName'));
Here's an example for both: http://jsfiddle.net/wwQ7h/
$('#someid').click( function() {
alert( this.tagName );
});
Some browsers may not do the click event on input. Instead you might want focus()
$('#someid').focus( function() {
alert( this.tagName);
});
精彩评论