jQuery标签的元素名称
我试着在jQuery中获取元素标记名称。
我有以下html:
New Revision 
This is a test paragraph.
- test
和javascript:
$(".edit").click(function(){ $(this).closest("div.section").children().each(function(){ alert($(this).tagName + " - " + $(this).html()); }); })
我已经尝试了$(this).tagName
, $(this).nodeName
和$(this).attr("tag")
,如此问题所述: jQuery可以提供标签名称吗?
但我总是得到undefined
的回报。 html()
正确输出。 为什么我不能获得每个元素的标记名称?
尝试
this.nodeName
而不是$(this).nodeName
this
指的是DOM元素本身, $(this)
将元素包装在jQuery对象中。
编辑
如果你需要Jquery方法,你可以使用
$(this).prop("tagName")
你有没有尝试过:
$(this).attr("id", "rnd" + this.nodeName.toLowerCase() + "_" + i.toString());
如链接问题中所述。 此外, $(this)与此之间存在很大差异
在浏览器控制台中尝试了这个并且它可以工作:
document.getElementsByTagName("a")[0].tagName // this uses javascript
这使用jquery:
$('a').get(0).nodeName; // this works for jquery
试试这个:
$(".edit").click(function(){ $(this).closest("div.section").children().each(function(){ alert(this.tagName + " - " + $(this).html()); }); })