最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Skycons, cant display the same icon twice? - Stack Overflow

programmeradmin3浏览0评论

I've hooked up the JavaScript plugin "Skycons" to Yahoo weather RSS feed. The problem I am having is that multiple days may have the same weather forecast and because the plugin pulls the icons from ID instead of class I am unable to pull the same icon a second time.

For example, all icons below will show apart from the last li instance - because I have repeated the id snow:

<ul class="days">
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Saturday</strong>
             <canvas id="snow" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Sunday</strong>
             <canvas id="rain" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Monday</strong>
             <canvas id="sleet" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Wednesday</strong>
             <canvas id="snow" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
</ul>

Here is my init JS:

<!--SkyCons-->  
<script type="text/javascript" src="js/vendors/skycons/skycons.js"></script> 
<script>
      var icons = new Skycons({"color": "#fff"}),
          list  = [
            "clear-day", "clear-night", "partly-cloudy-day",
            "partly-cloudy-night", "cloudy", "rain", "sleet", "snow", "wind",
            "fog"
          ],
          i;

      for(i = list.length; i--; )
        icons.set(list[i], list[i]);

      icons.play();
    </script> 

and here is a link to the referenced JS file:

.js

I've hooked up the JavaScript plugin "Skycons" to Yahoo weather RSS feed. The problem I am having is that multiple days may have the same weather forecast and because the plugin pulls the icons from ID instead of class I am unable to pull the same icon a second time.

For example, all icons below will show apart from the last li instance - because I have repeated the id snow:

<ul class="days">
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Saturday</strong>
             <canvas id="snow" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Sunday</strong>
             <canvas id="rain" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Monday</strong>
             <canvas id="sleet" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
       <li class="col-md-3 col-sm-3 col-xs-3"><strong>Wednesday</strong>
             <canvas id="snow" width="50" height="50"></canvas>
             <span>19&deg;</span>
       </li>
</ul>

Here is my init JS:

<!--SkyCons-->  
<script type="text/javascript" src="js/vendors/skycons/skycons.js"></script> 
<script>
      var icons = new Skycons({"color": "#fff"}),
          list  = [
            "clear-day", "clear-night", "partly-cloudy-day",
            "partly-cloudy-night", "cloudy", "rain", "sleet", "snow", "wind",
            "fog"
          ],
          i;

      for(i = list.length; i--; )
        icons.set(list[i], list[i]);

      icons.play();
    </script> 

and here is a link to the referenced JS file:

https://github./darkskyapp/skycons/blob/master/skycons.js

Share Improve this question edited Jul 4, 2014 at 11:41 Gabriele Petrioli 196k34 gold badges271 silver badges328 bronze badges asked Jul 4, 2014 at 10:19 Tom HughesTom Hughes 835 bronze badges 2
  • What is the script you run to initialize the plugin ? – Gabriele Petrioli Commented Jul 4, 2014 at 10:28
  • Please see above, I have edited the original post to include this – Tom Hughes Commented Jul 4, 2014 at 10:30
Add a ment  | 

1 Answer 1

Reset to default 10

You should use the version that works with element references instead of id

(And change the html to use classes instead of ids)

for(i = list.length; i--; ) {
    var weatherType = list[i],
        elements = document.getElementsByClassName( weatherType );
    for (e = elements.length; e--;){
        icons.set( elements[e], weatherType );
    }
}

and change your html to

   <li class="col-md-3 col-sm-3 col-xs-3"><strong>Saturday</strong>
         <canvas class="snow" width="50" height="50"></canvas>
         <span>19&deg;</span>
   </li>
   <li class="col-md-3 col-sm-3 col-xs-3"><strong>Sunday</strong>
         <canvas class="rain" width="50" height="50"></canvas>
         <span>19&deg;</span>
   </li>
   <li class="col-md-3 col-sm-3 col-xs-3"><strong>Monday</strong>
         <canvas class="sleet" width="50" height="50"></canvas>
         <span>19&deg;</span>
   </li>
   <li class="col-md-3 col-sm-3 col-xs-3"><strong>Wednesday</strong>
         <canvas class="snow" width="50" height="50"></canvas>
         <span>19&deg;</span>
   </li>

If you need to support IE8 and older you need to use this polyfill for the getElementsByClassName function: Polyfill for getElementsByClassName for particular uses

发布评论

评论列表(0)

  1. 暂无评论