2015-06-11 66 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}) 
     } 

的作品就好了。