2017-06-29 58 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时,我将不得不在'='上分割。尽管这很有效!感谢您的快速帮助! –