如何使用jQuery获得div的当前类?

时间:2023-02-04 20:36:28

Using jQuery, how can I get the current class of a div called div1?

使用jQuery,如何获得名为div1的div的当前类?

11 个解决方案

#1


105  

Just get the class attribute:

得到class属性:

var div1Class = $('#div1').attr('class');

Example

例子

<div id="div1" class="accordion accordion_active">

To check the above div for classes contained in it

检查上面的div中包含的类

var a = ("#div1").attr('class'); 
console.log(a);

console output

控制台输出

accordion accordion_active

#2


17  

Simply by

简单地通过

var divClass = $("#div1").attr("class")

You can do some other stuff to manipulate element's class

您可以做一些其他的事情来操作元素的类

$("#div1").addClass("foo"); // add class 'foo' to div1
$("#div1").removeClass("foo"); // remove class 'foo' from div1
$("#div1").toggleClass("foo"); // toggle class 'foo'

#3


11  

$('#div1').attr('class')

will return a string of the classes. Turn it into an array of class names

将返回一个类的字符串。将它转换为类名的数组

var classNames = $('#div1').attr('class').split(' ');

#4


7  

From now on is better to use the .prop() function instead of the .attr() one.

从现在开始,最好使用.prop()函数而不是.attr()函数。

Here the jQuery documentation:

这里的jQuery文档:

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.

对于jQuery 1.6, .attr()方法返回未设置的属性的未定义。要检索和更改DOM属性,请使用.prop()方法。

var div1Class = $('#div1').prop('class');

#5


2  

$("#div1").attr("class")

#6


1  

var classname=$('#div1').attr('class')

#7


1  

var className=$('selector').attr('class');

or

var className=$(this).attr('class');

the classname of the current element

当前元素的类名。

#8


1  

The addClass method in jQuery has a currentClass built in. You can use it inside a function call. Like so:

jQuery中的addClass方法内置了一个currentClass。您可以在函数调用中使用它。像这样:

<div>First div</div>
<div class="red">Second div</div>
<div>Third div</div>
<div>Fourth div</div>

<script>
  $("div").addClass(function(index, currentClass) {
    var addedClass;
    if ( currentClass === "red" ) {
      addedClass = "green"; }
    return addedClass;
  });
</script>

#9


0  

try this

试试这个

$("#div1").attr("class")

$(" # div1”).attr(类)

#10


0  

<div  id="my_id" class="my_class"></div>

if that is the first div then address it like so:

如果这是第一个div,那么就这样称呼它:

document.write("div CSS class: " + document.getElementsByTagName('div')[0].className);

alternatively do this:

或者这样做:

document.write("alternative way: " + document.getElementById('my_id').className);

It yields the following results:

其结果如下:

div CSS class: my_class
alternative way: my_class

#11


0  

if you want to look for a div that has more than 1 class try this:

如果你想找一个有超过一个类的div,试试这个:

Html:

Html:

<div class="class1 class2 class3" id="myDiv">

Jquery:

Jquery:

var check = $( "#myDiv" ).hasClass( "class2" ).toString();

ouput:

输出:

true

真正的

#1


105  

Just get the class attribute:

得到class属性:

var div1Class = $('#div1').attr('class');

Example

例子

<div id="div1" class="accordion accordion_active">

To check the above div for classes contained in it

检查上面的div中包含的类

var a = ("#div1").attr('class'); 
console.log(a);

console output

控制台输出

accordion accordion_active

#2


17  

Simply by

简单地通过

var divClass = $("#div1").attr("class")

You can do some other stuff to manipulate element's class

您可以做一些其他的事情来操作元素的类

$("#div1").addClass("foo"); // add class 'foo' to div1
$("#div1").removeClass("foo"); // remove class 'foo' from div1
$("#div1").toggleClass("foo"); // toggle class 'foo'

#3


11  

$('#div1').attr('class')

will return a string of the classes. Turn it into an array of class names

将返回一个类的字符串。将它转换为类名的数组

var classNames = $('#div1').attr('class').split(' ');

#4


7  

From now on is better to use the .prop() function instead of the .attr() one.

从现在开始,最好使用.prop()函数而不是.attr()函数。

Here the jQuery documentation:

这里的jQuery文档:

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.

对于jQuery 1.6, .attr()方法返回未设置的属性的未定义。要检索和更改DOM属性,请使用.prop()方法。

var div1Class = $('#div1').prop('class');

#5


2  

$("#div1").attr("class")

#6


1  

var classname=$('#div1').attr('class')

#7


1  

var className=$('selector').attr('class');

or

var className=$(this).attr('class');

the classname of the current element

当前元素的类名。

#8


1  

The addClass method in jQuery has a currentClass built in. You can use it inside a function call. Like so:

jQuery中的addClass方法内置了一个currentClass。您可以在函数调用中使用它。像这样:

<div>First div</div>
<div class="red">Second div</div>
<div>Third div</div>
<div>Fourth div</div>

<script>
  $("div").addClass(function(index, currentClass) {
    var addedClass;
    if ( currentClass === "red" ) {
      addedClass = "green"; }
    return addedClass;
  });
</script>

#9


0  

try this

试试这个

$("#div1").attr("class")

$(" # div1”).attr(类)

#10


0  

<div  id="my_id" class="my_class"></div>

if that is the first div then address it like so:

如果这是第一个div,那么就这样称呼它:

document.write("div CSS class: " + document.getElementsByTagName('div')[0].className);

alternatively do this:

或者这样做:

document.write("alternative way: " + document.getElementById('my_id').className);

It yields the following results:

其结果如下:

div CSS class: my_class
alternative way: my_class

#11


0  

if you want to look for a div that has more than 1 class try this:

如果你想找一个有超过一个类的div,试试这个:

Html:

Html:

<div class="class1 class2 class3" id="myDiv">

Jquery:

Jquery:

var check = $( "#myDiv" ).hasClass( "class2" ).toString();

ouput:

输出:

true

真正的