2015-06-11 62 views
0

我试图动态地改变maphilight的颜色。jQuery maphilight动态地改变颜色

reponse = JSON.parse(reponse); 
$('area').each(function() { 
    $(this).attr('data-maphilight', '{"stroke":0, "fillColor":"CCFFCC", "fillOpacity":0.3, "alwaysOn":true}'); 
    // POUR LA CRÉATION DE BD, METS TOUS LES TERRAIN DANS COMMENTAIRES AVEC UN TYPE 1 
    // $('#commentaireArea').val(($('#commentaireArea').val().concat("('"+$(this).attr('id')+"', 1), \n"))); 
      }) 

    for (var i = 0; reponse.length > i; i++) { 
     var test = '#'.concat(reponse[i].nomTerrain); 
     $(test).attr('data-maphilight', '{"stroke":0, "fillColor":"FF0000", "fillOpacity":0.3, "alwaysOn":true}'); 
    }    
$('img[usemap]').maphilight(); 

这只能使用一次。当谈到第二次,它不是!它改变了CSS类,但不是maphilight!

回答

1

Corect的方式来做到这一点是通过使用。数据(),而不是.attr()

   reponse = JSON.parse(reponse); 

      $('area').each(function() { 
       $(this).data('maphilight', {"stroke":0, "alwaysOn": true, "fillColor":"CCFFCC", "fillOpacity":0.3})     
       // POUR LA CRÉATION DE BD, METS TOUS LES TERRAIN DANS COMMENTAIRES AVEC UN TYPE 1 
       // $('#commentaireArea').val(($('#commentaireArea').val().concat("('"+$(this).attr('id')+"', 1), \n"))); 
      }) 

         for (var i = 0; reponse.length > i; i++) { 
       var id = '#'.concat(reponse[i].nomTerrain); 
       $(id).data('maphilight', {"stroke":0, "alwaysOn": true, "fillColor":"FF0000", "fillOpacity":0.3}) 
     } 

的作品就好了。