2017-06-29 57 views
0

我有一個互動式地圖,我正在使用rss訂閱源。地圖本身是在SVG中創建的,每個「檢查點」的類別爲circle,並增加爲circle+k(circle1,circle2,circle3等)。我的RSS提要將確定有多少前述圈將得到一類filled,以填補他們的顏色保存一個變量,特別是在jQuery中增加的div?

我編程做上述,我跑2個各功能來增加每個格,像這樣:

//increments circle class on the map's dots 
$("#map .circle").each(function(k) { 
    $(this).addClass("circle" + (k + 1)); 
    }); 


//increments step class on the rss-fed items 
$(".gs-rss-feed .hs-rss-item").each(function(i) { 
    $(this).addClass("step" + (i + 1)); 
    //if circle class + number = step class + number, add class filled to circle 
    $("#map .circle").eq(i).addClass("filled"); 

    }); 

但現在我需要做2多餘的東西,訪問來自各個RSS職位(titleurl)的某些信息。這些都是在課堂上都做了,所以我應該能夠得到像這樣的信息:

//Get link from html 
var link = $('.hs-rss-title').attr('href').split('='); 
//Get title 
var title = $('.hs-rss-title span').html(); 

那麼我就需要使用.wrap();


我的問題來包裝每一個圓圈在我的地圖我遇到麻煩的是,如何將每個變量(titleurl)都限定在每個特定的點上。如果我使用上述保存變量的方法,那麼嘗試使用$("#map .circle").eq(i).wrap("<a href="+link+"></a>");它最後只會添加第一個示例,即:將第一個RSS項目的url添加到地圖上的每個點。

他們是一個更好的方法,我可以將其範圍?這裏是我的codepen,任何幫助表示讚賞!

+0

您可以在元素上使用自定義數據屬性,然後再拉取它。像data-title =「Something」data-href =「http://www.google.com」 –

+0

由於它在技術上將成爲rss提要,所以我無法編輯html結構。 –

回答

1

我沒有足夠的聲譽作出評論,所以我不得不張貼此作爲一個答案..

你就不能這樣做內部的第二每個?

//increments step class on the rss-feed items 
$(".gs-rss-feed .hs-rss-item").each(function(i) { 
    var $rss-item = $(this); 
    $rss-item.addClass("step" + (i + 1)); 
    //if circle class + number = step class + number, add class filled to circle 
    var $circle = $("#map .circle").eq(i); 
    $circle.addClass("filled"); 

    //Get link from html 
    var link = $rss-item.find('.hs-rss-title').attr('href').split('='); 
    //Get title 
    var title = $rss-item.find('.hs-rss-title span').html(); 

    // here you can wrap $circle and do whatever you want with this link and title 
}); 

而不是真的與你的問題有關,但你爲什麼分裂href on =?除了缺少一些斜槓之外,我沒有看到給定的href屬性中需要將其分開的任何內容=。

+0

這是完美的,我沒有使用'.find()'這使得它無法確定範圍。這些URL現在只是虛擬鏈接,但是當它們來自我們的RSS時,我將不得不在'='上分割。儘管這很有效!感謝您的快速幫助! –