开发者

How to get class names?

Hey, I am trying to get the class names dynamically for the script below.

I have different class names that are created through PHP so need to get them using jQuery dynamically.

As you can see below, there seems to be 2 areas where I need to get the class name. At the moment it is hard coded (the classes are named 'result'):

1) var new_content = $('#hidd开发者_StackOverflowenresult div.result:eq('+page_index+')').clone();

2) var num_entries = $('#hiddenresult div.result').length;

so, I'd like jquery to get the class name instead of me just hard coding it in as seen below.

The jQuery file:

<script type="text/javascript">

        function pageselectCallback(page_index, jq){
        var new_content = $('#hiddenresult div.result:eq('+page_index+')').clone();
        $('#Searchresult').empty().append(new_content);
        return false;
    }

    /** 
     * Callback function for the AJAX content loader.
     */
    function initPagination() {
        var num_entries = $('#hiddenresult div.result').length;
        // Create pagination element
        $("#Pagination").pagination(num_entries, {
            num_edge_entries: 2,
            num_display_entries: 8,
            callback: pageselectCallback,
            items_per_page:1
        });
     }

    // Load HTML snippet with AJAX and insert it into the Hiddenresult element
    // When the HTML has loaded, call initPagination to paginate the elements        
    $(document).ready(function(){      
        initPagination();
    });
</script>

Any help on implementing a solution would be great. Thanks


If i understand correctly you want your code to work regardless of the class that is given to the divs. (but some class will be given)

If so use

var new_content = $('#hiddenresult div[class]:eq('+page_index+')').clone();

and

var num_entries = $('#hiddenresult div[class]').length;

which means find any div under #hiddenresult that has a class attribute defined (regardless of the actual class name..)


You can get the contents of the class attribute as a string like this:
$("#hiddenresult div").attr("class");
Note: this may not work for you if your element has multiple classes assigned to it.

You can also use this:
$("#hiddenresult div").hasClass("result");
To check if an element has a specific class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜