简体   繁体   English

使用jquery / javascript提取第一个div元素的类名

[英]extracting class name of first div element using jquery/javascript

i have a couple of div elements as string, is there any way to extract the name of the class of the first div element ? 我有几个div元素作为字符串,有没有办法提取第一个div元素的类的名称? I tried (#element).attr('class'). 我尝试了(#element).attr('class')。 it does not yield results,may be because it is a string.here is the sample div. 它不产生结果,可能是因为它是一个字符串。这是示例div。 PLease note that , it has just div tags and the type of the whole structure is string 请注意,它只有div标签,整个结构的类型是字符串

<div class="some class" ><div class="inner div"></div></div>

I want "some class" value. 我想要“某类”的值。 The typeof this whole thing is a string. 这整个东西的类型是一个字符串。 Thanks in advance 提前致谢

var str = '<div class="some class" ><div class="inner div"></div></div>';
console.log($(str).eq(0).attr('class'));

jsFiddle example jsFiddle示例

I have found the solution 我找到了解决方案

this is your example, for extract the first div use 这是您的示例,用于提取第一个div使用

alert($("div:first").attr("class")); alert($(“ div:first”)。attr(“ class”));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM