2013-06-29 71 views
-4

我試圖將內聯CSS添加到JS計算(例如3 * 2 * 1)以使其變爲紅色。 我已經嘗試了一個span標籤,但它只是顯示計算而不是答案。我也嘗試使用內部樣式表。我該怎麼做呢? 這是JS提琴:http://jsfiddle.net/ytWea/將CSS添加到JavaScript計算中

<html> 
    <head> 
     <script> 
      function volumeFields(){ 
       document.getElementById("idLengthVolume"); 
       document.getElementById("idWidthVolume"); 
       document.getElementById("idHeightVolume"); 
       document.getElementById("calcVolume"); 
      } 

      function computeVolume(){ 
       var x=document.getElementById("idLengthVolume").value; 
       var y=document.getElementById("idWidthVolume").value; 
       var z=document.getElementById("idHeightVolume").value; 
       var sVolume="The volume of your object is " + x * y * z 
       document.getElementById("idOutputVolume").innerHTML=sVolume; 
      } 
     </script> 
    </head> 
    <body onload="volumeFields()"> 
     Insert the length of your object:<input type="text" size="20" id="idLengthVolume" /> <br/> 
     Insert the width of your object:<input type="text" size="20" id="idWidthVolume" /><br/> 
     Insert the height of your object:<input type="text" size="20" id="idHeightVolume" /> 
     <input type="button" style="display:block" onclick="computeVolume()" value="Calculate" id="calcVolume"/> 
     <div id='idOutputVolume'></div> 
    </body> 
</html> 
+1

請分享您的代碼。沒有代碼就無法回答你的問題。 – Gus

+0

請閱讀這個問題,假裝你是我(對你的問題一無所知),並試圖回答這個問題。 –

+0

Gus,我添加了我正在使用的代碼 – vman

回答

1

至於內部樣式表,HTML的頭節點包括風格標籤,並把所有的CSS在那裏:

<html> 
    <head> 
     <style> 
      h1,p { 
       background-color:red; 
       } 
     </style> 
    </head> 
</html> 

抱歉,如果這不是你要找的東西,但是由於缺乏信息,這是我能做的最好的。

編輯:試試這個:

function computeVolume() { 
    var x = document.getElementById("idLengthVolume").length; 
    var y = document.getElementById("idWidthVolume").width; 
    var z = document.getElementById("idHeightVolume").depth; 
    var objvolume = x * y * z; 
    var sVolume = "The volume of your object is " + objvolume; 
    document.getElementById("idOutputVolume").innerHTML = sVolume; 
} 
+0

我加了代碼和JS小提琴 – vman

+0

@ bagava2看我的編輯。 –

+0

@ user2297366,但wheres的CSS – vman