gpt4 book ai didi

元素的 jQuery 标签名称

转载 作者:技术小花猫 更新时间:2023-10-29 12:35:40 25 4
gpt4 key购买 nike

我尝试在 jQuery 中获取元素标签名称。

我有以下 html:

<div class="section" id="New_Revision">

<h1>New Revision&nbsp<img alt="Lock_closed" class="edit" data-id="1" src="/assets/lock_closed.svg" /></h1>

<p>This is a test paragraph.</p>

<ol class="references">
<li>test</li>
</ol>
</div>

和javascript:

$(".edit").click(function(){
$(this).closest("div.section").children().each(function(){
alert($(this).tagName + " - " + $(this).html());
});
})

我已经尝试过 $(this).tagName$(this).nodeName$(this).attr("tag") 如本问题所述:Can jQuery provide the tag name?

但我总是得到 undefined 作为返回。 html() 输出正确。为什么获取不到每个元素的标签名?

最佳答案

尝试

this.nodeName 而不是 $(this).nodeName

this 指的是 DOM 元素本身,$(this) 将元素包装在一个 jQuery 对象中。

编辑

如果您需要 Jquery 方法,您可以使用

$(this).prop("tagName")

关于元素的 jQuery 标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989066/

25 4 0