简体   繁体   中英

get a specific div class from div and based on that do a function

the title might not explain my need well. Basically, beginning of the page, I have like 20 divs, each has id of cal001, cal002... to cal020.

Then at the end of page, I have a bunch of divs each have class like tocal001, or tocal002. Those divs might have different classes as well. Say I have a div with class tocal001, How can I make javascript recognize the specific class that start with tocal and clone().after() to the cal001 div?

To get the tocal... :

var tocal = $('[id^="tocal"]');

To append:

tocal.each(function(index, item){
     var $item = $(item);
     var id = item.id;
     var num = id.substr(id.length - 3, 3);
     $item.clone().insertAfter($('#cal' + num));
});

Here is a demo: http://jsfiddle.net/maniator/gRhCC/

$('[id^="tocal"]').each(function(){
    var tmpid = this.id.replace('to','');
    $(this).clone().insertAfter($('[id$="' + tmpid + '"]'));
    $(this).remove();
});

Demo: http://jsfiddle.net/AlienWebguy/4TK5d/

If you want to remove the duplicates and simply "shuffle" the rows:

$('[id^="tocal"]').each(function(){
    var tmpid = this.id.replace('to','');
    $(this).remove().insertAfter($('[id$="' + tmpid + '"]'));
});

Demo: http://jsfiddle.net/AlienWebguy/4TK5d/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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