2013-12-09 43 views
2

我看起來很高和低,無法找出這一個!jQuery推送,彈出和包裝值

在頁面上,我有鏈接有一個特定的類(plmore)。在同一頁面上,我擁有一個擁有特定類別(fcontainer)的div。與類plmore相關的鏈接數總是等於使用fcontainer類的div數。

我的問題:

我需要來包裝與使用plmore找到的鏈接fcontainer類的div。

僞代碼:的DIV ID的HREF中
GET數組
GET數組
WRAP的div的HREF

這是我迄今爲止 - 我在jQuery的相當新的,我不能確定的語法爲這一個。任何人都可以幫忙嗎?我將不勝感激!

<script> 
    jQuery(document).ready(function($) { 

    var hrefs = new Array();  
    $('a.plmore').each(function(){ 
     hrefs.push($(this).find('a').attr('href')); 
    }); 

    var features = new Array(); 
    $('fcontainer').each(function(){ 
     features.push($(this).find('div').attr('id')); 
    }); 

    /* how does one pop from both arrays and wrap?? */ 
    }); 
</script> 

非常感謝!

回答

2

你的意思是像

jQuery(function ($) { 
    //find all the target anchor elements 
    var $as = $('a.plmore'); 

    //find the div elements 
    $('.fcontainer').each(function (idx) { 
     //wrap the div at index idx using the href value of anchor element at index idx 
     $(this).wrap($('<a/>', { 
      href: $as.eq(idx).attr('href') 
     })) 
    }); 
}); 

演示:Fiddle

+0

它是如此接近! 現在肯定包裝了div - 感謝你的這個! href屬性似乎沒有采取(它不會被添加到標記)。任何線索爲什麼這可能是? – user3083452

+0

它還活着!你真了不起!非常感謝!這個DIV有很多DIV ...我只需要定位一個不同的DIV。非常感謝! – user3083452

相關問題