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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1536117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:39:54  来源:igfitidea点击:

How can I get the current class of a div with jQuery?

jquery

提问by Shyju

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

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

回答by CMS

Just get the class attribute:

只需获取类属性:

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

回答by Jakub Arnold

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'

回答by Russ Cam

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

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

将返回一个字符串的类。把它变成一个类名数组

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

回答by mt81

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() 方法为尚未设置的属性返回 undefined。此外,.attr() 不应用于普通对象、数组、窗口或文档。要检索和更改 DOM 属性,请使用 .prop() 方法。

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

回答by Tzury Bar Yochay

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

回答by halocursed

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

回答by Stefan Gruenwald

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>

回答by aymen

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

or

或者

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

the classname of the current element

当前元素的类名

回答by Stefan Gruenwald

<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

回答by Badiparmagi

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

如果您想查找具有 1 个以上类的 div,请尝试以下操作:

Html:

网址:

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

Jquery:

查询:

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

ouput:

输出:

true

true