<body> <div id="f1"></div> <script type="text/javascript"> var a=[]; for (var i = 1; i <366; i++) { a.push(' '+i+' '); } if(a[i]%7==0){ document.getElementById('f1').style.color='Blue'; } console.log(a); document.getElementById('f1').innerHTML=a; </script> </body> |
2 answers
var a = []; for (var i = 1; i < 366; i++) { a.push(" " + i + " "); const div = document.createElement("div"); div.innerHTML = i; if (i % 7 === 0) { div.style.color = "blue"; } document.getElementById('f1').appendChild(div); } <div id="f1"></div> |
var a = []; for (var i = 1; i < 366; i++) { a.push('<span style="color:' + (i % 7 ? 'black' : 'blue') + '">' + i + '</span>'); } document.getElementById('f1').innerHTML = a.join(", "); <div id="f1"></div> - Wow approach. Maybe even use
eval? Creating an element as a string, then inserting this string into the layout is a bad idea. - Alexandr Tovmach 7:27 pm - @AlexandrTovmach if the script performs the task, it has the right to life. - UModeL
- @UModeL oh well? I repeat, can eval be used too normally? - Alexandr Tovmach
|