开发者

Get numerical value at end of class name and store as variable using jQuery

开发者 https://www.devze.com 2023-03-21 00:47 出处:网络
Consider this menu output by a WordPress function: <ul id=\"header-cats\"> <li class=\"cat-item cat-item-6\"><a href=\"url\" title=\"View all posts filed under Category I\">Catego

Consider this menu output by a WordPress function:

<ul id="header-cats"> 

    <li class="cat-item cat-item-6"><a href="url" title="View all posts filed under Category I">Category I</a> 
</li> 
    <li class="cat-item cat-item-7"><a href="url" title="View all posts filed under Category II">Category II</a> 
</li> 
    <li class="cat-item cat-item-8"><a href="url" title="View all posts filed under Category III">Category III</a> 
</li> 

</ul> 

Now consider this list of posts:

<ul id="posts-preview" class="clearfix"> 

    <li class="filter-reset filter-hide filter-6 ">             
        <a class="post-thumb" id="post-112" href="url" >Link</a>        
    </li>

    <li class="filter-reset filter-hide filter-6 filter-8 ">            
        <a class="post-thumb" id="post-102" href="url" >Link</a>        
    </li>

    <li class="filter-reset filter-hide filter-7 ">             
        <a class="post-thumb" id="post-88" href="url" >Link</a>         
    </li>

    <li class="filter-reset filter-hide filter-6 ">             
        <a class="post-thumb" id="post-6" href="url" >Link</a开发者_如何学Python>      
    </li>
</ul>

My aim is to use a jQuery function to extract the numerical ending of the menu's class name (ie. the 6 in cat-item 6) and use that value to target the corresponding post. To elaborate I would use that 6 as a variable and find the filter class that ends in 6.

Here is what I have so far:

  $('#header-cats li').click(function(){  

    var num_id = $(this).attr('class') // ... matching "cat-item-?" etc...

        $(".filter-"+num_id).fadeIn(500); 

        return false;

  });

Should be easy for a js fiend :-)


You could use a RegEx to get the number from your class id (code below not tested) -

$('#header-cats li').click(function(){  
    var num_id = $(this).attr('class').match(/\d+/); // ... matching "cat-item-?" etc...
    $(".filter-"+num_id).fadeIn(500); 
    return false;
});


If you are sure there is only 1 number in the class string you could use this:

var num_id = $(this).attr('class').match(/\d+/)[0]

if you are not sure, better use:

var num_id = $(this).attr('class').match(/cat-item-(\d+)/)[1]


I usually use .substring(9) to get the number, but i guess there's a better way than that

also i would either put that in a data-attribute value or an id for easier reference like so:

<li class="cat-item" data-catitem-num=8><a href="url" title="View all posts filed under Category III">Category III</a> 

then in jquery:

$(".cat-item").click(function(){
  var filterNum = $(this).attr("data-catitem-num");
  $(".filter-"+filterNum).fadeIn(500);
  return false;
})

or something to that effect

note if your element will have a unique "id" don't put it in a class. put it in an id or data-attribute(so in your case, catitem and filternum would either be in data-catitem or data-filternum OR in their element ids.

0

精彩评论

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