开发者

Get class name dynamically with jquery

开发者 https://www.devze.com 2022-12-15 07:55 出处:网络
I am trying to get this class name dynamically in jquery: echo \"<div id=\\\"maini\\\" class=\\\"type_\" .$category. \" start_\" .$adSize. \" color_\" .$color. \"\\\"> \\n\";

I am trying to get this class name dynamically in jquery:

echo "<div id=\"maini\" class=\"type_" .$category. " start_" .$adSize. " color_" .$color. "\"> \n";
     echo " " . $siteName . " \n";  
echo "</div> \n";

as you can see the class name is generated with PHP.

I need the class name for this snippet of code. But what I tried does not work -> $("div." + this.id). :

    $(document).ready(function() {
        maini_s = $("div." + this.id).remove();
        num_of_arts = maini_s.length;
        ipp = 3;

Here is the entire js file in case you need to see the other elements before.

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();
    f开发者_运维技巧or(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." + this.id).remove();
    num_of_arts = maini_s.length;
    ipp = 3;


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


To get the classname, just pull the class attribute:

var className = $(".element").attr("class");

Be sure that you take into consideration that sometimes elements have multiple class names, seperated by spaces.


I may be missing something but the syntax...

"div." + this.id

would be looking for DIVs with the class name contained in this.id, not the ID. If you want to select by ID, I believe you'd want...

"div#" + this.id

which would use the # ID selector and not the . class selector.


you call maini_s = $("div." + this.id).remove(); from the $(document).ready(function()

The this keyword in this case refers to the window.document element .. which normally has no id ..

are you sure you want to run it from there ? or perhaps a click event on some other item ?

0

精彩评论

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