Get the current element type
How can I find the el tag nam开发者_运维知识库e below (eg. div, p or span) ?
$('div, p, span').each(function(){
var el = $(this);
});
Use the tagName
property:
$('div, p, span').each(function(){
var tag = this.tagName;
});
Note: tagName
is a property of the element, not a jQuery method.
Using nodeName
is better than tagName
if you consider consistency between browsers.
This has good explanation: Difference between .tagName and .nodeName
$('div, p, span').each(function(){
var el = this.nodeName;
});
<script>
$(function(){
$("p,div").bind('click',function(){
var el = this.tagName;
alert(el);
});
});
</Script>
<p>p</p>
<div>div</div>
....
more info (example) here
You can use
$(this).prop('tagName')
or
$(e.target).prop('tagName')
to get the selected tag name.
精彩评论