1

$(html).hasClass("class"), $(document).hasClass("class"), $(document.html).hasClass("class") None works. What selector should I use to get the class of the tag ? Also, how can I find a list of standard elements which I can select in jQuery ?

4

5 回答 5

6

Try this one:

$('html').hasClass("class");
  • [!] You forgot to type quotation around html.

Here you can find all jQuery Selectors.


Extra Information

The .hasClass() method will return boolean. You can access the class name by getting the class attribute with .attr() method:

<div class="a" id="example1"></div>
var c = $('#element').attr('class'); // (string) a

If your element has multiple classes, you can split the string to convert it to an array:

<div class="a b c" id="example2"></div>
var k = $('#example2').attr('class').split(/\s+/); // [a, b, c]

Here is the FIDDLE DEMO.

于 2013-05-16T09:37:42.943 回答
6

"What selector should I use to get the class of the tag?"

The other answers use hasClass(), which returns a boolean not a class:

$('html').attr('class');
于 2013-05-16T09:39:39.173 回答
3
<html class="someClass">
</html>

Do this -

$('html').hasClass('someClass');
于 2013-05-16T09:38:18.223 回答
3

Your DOM is structured like this, basically:

- document
  - documentElement
    - head
    - body

You can either use jQuery's internal selector engine by writing $('html').hasClass('class') or you can target the HTML element yourself by writing $(document.documentElement).hasClass('class').

于 2013-05-16T09:41:27.073 回答
0

If your <div> has an id:

​&lt;div id="test" class="my-custom-class"></div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

...you can try:

var yourClass = $("#test").prop("class");

If your has only a class, you can try:

var yourClass = $(".my-custom-class").prop("class");
于 2013-05-16T09:39:47.970 回答