简体   繁体   English

如何从jQuery中的多个图标调用图标?

[英]How to call icons from multiple icons in jquery?

I have these js code; 我有这些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); 


      });
        }
  });

  });
  }

Here is some html part: 这是一些html部分:

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

First when the page is loaded the combobox is filled with Station 1 ,Station2 etc.The when I want to select one of them (this refer 1, 2, 3, 4 or 5 ) I am calling sta_callStation function.I dynamically create person icons giving them some attributes, for example their ids; 首先,当页面被加载时,组合框充满了Station 1,Station2等。当我想选择其中一个(这是指1、2、3、4或5)时,我正在调用sta_callStation函数。我动态地创建人形图标给他们一些属性,例如他们的ID; id_1 ,id_2 etc.But after that how can I trigger them when mouse over, should I use class or id of them? id_1,id_2等。但是之后,如何在鼠标悬停时触发它们,我应该使用它们的类或ID? Before choosing station: https://docs.google.com/file/d/0B1RbKsJ4B8eoeW5wdWhLQW85QTQ/edit?usp=sharing After choosing one station(randomly produced passengers in php) https://docs.google.com/file/d/0B1RbKsJ4B8eoQmYwWVpYbno2NnM/edit?usp=sharing 选择车站之前: https : //docs.google.com/file/d/0B1RbKsJ4B8eoeW5wdWhLQW85QTQ/edit?usp= sharing选择一个车站后(PHP中随机产生的乘客) https://docs.google.com/file/d/ 0B1RbKsJ4B8eoQmYwWVpYbno2NnM / edit?usp =分享

You could use some client side UI framework (eg. Knockout.js). 您可以使用一些客户端UI框架(例如Knockout.js)。

If you would like to keep your UI rather simple, then yes as you said, you could create class with person attributes and appended reference to icon HTML element. 如果您想使UI保持简单,则可以像您所说的那样,创建带有person属性的类,并将引用附加到图标HTML元素上。 When creating instance of class, link HTML callback function to your object function. 创建类的实例时,将HTML回调函数链接到您的对象函数。

Somethig like this: 这样的东西:

var PassInfo = function (name)
{
  name: name,
  onhover: function ()
  {
    // do something special
  }
};

var passInstance = new PassInfo('passenger1');

$(pas_icon).hover(function()
{
  passInstance.onhover();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM