I'm working on a WordPress theme, and during the loop, it puts the_ID on each class. So, I have like a couple posts with the class "teamPost-138", "teamPost-222", etc...
I have stored that "teamPost-###" in a jQuery variable, and now I need to take off the "teamPost-" part. Is there a way I开发者_运维问答 can cut that in the variable, or can I just select the number part from the class in the HTML?
I'm not entirely clear on what your doing, is it just to assign the number to the variable instead of the teamPost-###?
Below is some code that you may be able to repurpose. For every element that has a class starting with 'teamPost-' it logs (to the firebug console) the number from their class. The index of isused to negate issues of the item having multiple classes.
e.g.
$('[class^=teamPost-]').each(function () {
var class = $(this).attr('class');
var numStart = class.indexOf('teamPost-')+9;
console.log(class.substring(numStart,numStart+3));
});
If its simply stripping the start of the variable then you just need the strubgstring part:
'teamPost-123'.substring(9); // equals '123'
$('#teamPost-###').attr('id', '###');
Changes the ID of the selected item.
精彩评论