2014-01-07 34 views
1

的javascript變量我在HTML有這樣的名單:顯示HTML

<div style="padding-left:30px;"> 
     <ul style="list-style-type:circle; margin-top:0px;"> 
       <li><a href="http://printoriente.com/tarjetas-presentacion/">Tarjetas de presentacion</a><label>I want the JS variable HERE</label></li> 
       <li><a href="http://printoriente.com/afiches/">Afiches</a><label>I want the JS variable HERE</label></li> 
       <li><a href="http://printoriente.com/volantes/">Volantes</a><label>I want the JS variable HERE</label></li> 
       <li><a href="http://printoriente.com/fotos/">Fotos</a><label>I want the JS variable HERE</label></li> 
</ul> 
</div> 

我需要在我所指示的(data.refPrice)變量 「我希望JS變量HERE」:

<script> 

$.getJSON("http://api.printoriente.com/price/tarjetas-presentacion", function(data) { 
    console.log(data.refPrice); 
}); 

$.getJSON("http://api.printoriente.com/price/afiches", function(data) { 
    console.log(data.refPrice); 
}); 

$.getJSON("http://api.printoriente.com/price/volantes", function(data) { 
    console.log(data.refPrice); 
}); 

$.getJSON("http://api.printoriente.com/price/Fotos", function(data) { 
    console.log(data.refPrice); 
}); 

</script> 
+0

儘可能避免硬編碼CSS樣式 –

回答

2

創建具有唯一ID的標籤或跨度作爲佔位符。然後設置範圍的內容

$('#yourid1').html(data.refPrice) 
0

分配一個ID,你的<a>,然後用html()text()

<li><a id="myId" href="http://printoriente.com/tarjetas-presentacion/">Tarjetas de presentacion</a><label>I want the JS variable HERE</label></li> 

$.getJSON("http://api.printoriente.com/price/tarjetas-presentacion", function(data) { 
    $("#myId").html(data.refPrice); 
}); 
+0

其作品購買我做到了,並與網站的其他部分產生衝突。我需要另一種解決方案 – nandophillips

+0

如果你有衝突,你有ID的問題。你應該真的明白爲什麼... – sdespont

1

的一種方式做到這一點從我的頭頂:

<li id="afiches"><a href="http://printoriente.com/afiches/">Afiches</a><label>I want the JS variable HERE</label></li> 

然後在你的js中:

$.getJSON("http://api.printoriente.com/price/afiches", function(data) { 
    console.log(data.refPrice); 
    $('#afiches').html(data.refPrice); 
}); 
+0

其作品買我做到了,並與其他網站產生衝突。我需要另一種解決方案 – nandophillips

+0

小心解釋它產生的衝突嗎? – user1477388