Guest User

Untitled

a guest
Aug 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. Dynamically changing background color fails
  2. newEle.style.background = "-webkit-gradient(linear, left top, right top, from(#2F2727), to(#FF0000))";
  3.  
  4. <html>
  5. <head>
  6. </head>
  7. <body>
  8.  
  9. <div id="mainContent">
  10. <p id="test">abcdef</p>
  11. </div>
  12.  
  13. </body>
  14. <script type="text/javascript">
  15. <!--
  16.  
  17. function decimalToHex( num )
  18. {
  19. // num is usually a decimal color in form ARGB
  20.  
  21. if (num == null || num == "undefined") { return "#FFFFFF"; }
  22.  
  23. var intNum = (parseInt(num,10)) & 0x00FFFFFF;
  24. return "#"+intNum.toString(16);
  25. }
  26.  
  27. var newEle = document.createElement("p");
  28. newEle.style.backgroundColor = decimalToHex(0); // this fails doesn't set the background color
  29. //newEle.style.backgroundColor = "#FF0000"; // But this works & sets it to red. Whats wrong with my function?!
  30. newEle.innerHTML = "kjdskjdkgj";
  31. document.getElementById("mainContent").appendChild(newEle);
  32. -->
  33. </script>
  34. </html>
  35.  
  36. var s = intNum.toString(16);
  37. while(s.length < 6) s = "0" + s;
  38. return "#" + s;
  39.  
  40. var intNum = '' + (parseInt(num,10)) & 0x00FFFFFF;
  41. intNum = intNum.toString(16);
  42. if (intNum.length < 2) intNum = '0' + intNum;
  43. return intNum;
  44.  
  45. newEle.style.backgroundColor = '#' + decimalToHex(255) +
  46. decimalToHex(0) + decimalToHex(0);
Add Comment
Please, Sign In to add comment