简体   繁体   English

如何使用jQuery获取类名?

[英]How to get the class names using jquery?

Hey, I'm wondering how I can get the class names dynamically using jquery for the script below. 嘿,我想知道如何使用jquery为以下脚本动态获取类名称。

The HTML output looks like this: HTML输出如下所示:

<div id="main-info-1" class="maini">
     <p>this is a paragraph.</p>
</div>

So, I'm trying to get the class name dynamically instead of hard coded like it is above. 因此,我正在尝试动态获取类名,而不是像上面那样进行硬编码。

There are two parts where I need to get the class names in the jquery script: 我需要在jquery脚本中获取类名的两部分:

1.) pc.children('div.maini').remove();

2.) maini_s = $('div.maini').remove();

As you can see the class 'maini' is hard coded and im unsure how to get the class name dynamically and put it properly in the script. 如您所见,类“ maini”是硬编码的,不确定如何动态获取类名称并将其正确地放入脚本中。

The jQuery file: jQuery文件:

<script type="text/javascript">   
// make them global to access them from the console and use them
// in handlePaginationClick
var maini_s;
var num_of_arts;
var ipp;

function handlePaginationClick(new_page_index, pagination_container) {
    var pc = $(pagination_container);
    pc.children('div.maini').remove();
    for(var i=new_page_index*ipp; i < (new_page_index+1)*ipp ;i++) {
        if (i < num_of_arts) {
                pc.append(maini_s[i]);
        }
    }
    return false;
}

$(document).ready(function() {
    maini_s = $('div.maini').remove();
    num_of_arts = maini_s.length;
    ipp = 3;

    // First Parameter: number of items
    // Second Parameter: options object
    $("#News-Pagination").pagination(6, {
        items_per_page:ipp, 
        callback:handlePaginationClick
    });
});


        </script>

Any help on this would be awesome, thank you. 对此,任何帮助都会很棒,谢谢。

It depends on what you're after exactly. 这完全取决于您所追求的。

You can get the class names using jQuery or plain javascript: 您可以使用jQuery或纯JavaScript获取类名称:

// jQuery
$myElement.attr('class');

// javascript
myElement.className

This will give you the string value of the class attribute of the element. 这将为您提供元素的class属性的字符串值。 If the element has more than one class (eg: <div class="foo bar"> ) the above methods will return "foo bar" . 如果元素具有多个类(例如: <div class="foo bar"> ),则上述方法将返回"foo bar" If you want an array of classes on an element, then you just need to split the result on whitespace: 如果要在元素上使用类数组,则只需要在空白处拆分结果:

var classes = myElement.className.split(/\s+/);
// classes = ["foo", "bar"]

使用jQuery获取该类名的方式如下:

$('#main-info-1').attr('class');

You can detect, add and remove classes without having to worry about splitting on whitespace when using multiple classes. 您可以检测,添加和删除类,而不必担心在使用多个类时在空格上进行拆分。 And there's a convenient toggle too: 还有一个方便的切换:

// toggle example
$('ele').toggleClass('active');

// toggle with optional bool parameter
$('ele').toggleClass('active', isActive );

// detect/add/remove classes without having to split on blank space " "
if ($('ele').hasClass('dull')) 
    $('ele').removeClass('dull').removeClass('productive');
else
    $('ele').addClass('fun').addClass('profitable');

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

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