【问题标题】:How to call icons from multiple icons in jquery?如何从jquery中的多个图标中调用图标?
【发布时间】:2013-04-12 07:00:51
【问题描述】:

我有这些js代码;

 $(document).ready(function() {
      $.getJSON("Stations.php", function(jsonData){  
      $.each(jsonData, function(key,value) {
      $('#selectStation')
     .append($("<option></option>")
     .attr("value",key)
     .text(value.name)); 

  });
 });
 });
function sta_callStation(sel){
  $('#noOfPassengers, #infoOfPassengers, #distType,#distParams').empty();
   $('#sta_numberOfIcons').empty();
    $.getJSON('Stations.php', function(station){

        $.each(station, function(sta_key, sta_value) {

        if(sel.value==sta_key)
        {
          $.each(sta_value.passengers, function(j,passengers) 
        {

          //$('#sta_numberOfIcons').append('<i class="icon-user" ></i>');
          var pas_icon = document.createElement("a");
          pas_icon.className = "icon-user";
          pas_icon.id='id_'+(j);
          pas_icon.setAttribute('href', '#');
          pas_icon.setAttribute('rel', 'popover');
          //alert('id_'+(j));
          document.getElementById('sta_numberOfIcons').appendChild(pas_icon); 


      });
        }
  });

  });
  }

这是一些 html 部分:

Stations
 <select name="selectStation" id="selectStation" onchange="sta_callStation(this);">   
 </select>

首先,当页面加载时,组合框填充有 Station 1 、Station2 等。当我想选择其中一个(指 1、2、3、4 或 5)时,我正在调用 sta_callStation 函数。我动态创建人物图标,赋予他们一些属性,例如他们的 id; id_1 ,id_2 等。但是之后我如何在鼠标悬停时触发它们,我应该使用它们的类还是 id? 选站前: https://docs.google.com/file/d/0B1RbKsJ4B8eoeW5wdWhLQW85QTQ/edit?usp=sharing 选择一站后(php中随机产生的乘客) https://docs.google.com/file/d/0B1RbKsJ4B8eoQmYwWVpYbno2NnM/edit?usp=sharing

【问题讨论】:

    标签: javascript html css jquery icons


    【解决方案1】:

    您可以使用一些客户端 UI 框架(例如 Knockout.js)。

    如果您想保持您的 UI 相当简单,那么正如您所说的,您可以创建具有个人属性的类并附加对图标 HTML 元素的引用。创建类实例时,将 HTML 回调函数链接到您的对象函数。

    类似这样的:

    var PassInfo = function (name)
    {
      name: name,
      onhover: function ()
      {
        // do something special
      }
    };
    
    var passInstance = new PassInfo('passenger1');
    
    $(pas_icon).hover(function()
    {
      passInstance.onhover();
    });
    

    【讨论】:

    • 好的,我明白了,但是当遇到其中一个图标时如何调用图标,这就是问题所在?:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2015-10-08
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 2017-11-07
    相关资源
    最近更新 更多