使用 JavaScript 根据输入值调整输入字段 (HTML) 的 (CSS) 中的文本颜色
P粉022140576
P粉022140576 2023-09-03 09:53:55
0
1
393
<p>我有一段代码可以从用户那里获取 10 个输入值(整数和浮点数):</p> <pre class="brush:php;toolbar:false;">var in1 = parseInt(document.getElementById(&quot;in1&quot;).value); var in2 = parseInt(document.getElementById(&quot;in2&quot;).value); var in3 = parseFloat(document.getElementById(&quot;in3&quot;).value); var in4 = parseFloat(document.getElementById(&quot;in4&quot;).value); var in5 = parseInt(document.getElementById(&quot;in5&quot;).value); var in6 = parseInt(document.getElementById(&quot;in6&quot;).value); var in7 = parseInt(document.getElementById(&quot;in7&quot;).value); var in8 = parseFloat(document.getElementById(&quot;in8&quot;).value); var in9 = parseFloat(document.getElementById(&quot;in9&quot;).value); var in10 = parseInt(document.getElementById(&quot;in10&quot;).value);</pre> <p>然后,我将所有输入放入一个数组中,以便更容易使用它们:</p> <pre class="brush:php;toolbar:false;">var arr = [in1, in2, in3, in4, in5, in6, in7, in8, in9, in10];</pre> <p>这个想法是通过迭代值来调整 HTML 容器的 CSS,并调整特定容器,其值等于 0。</p> <pre class="brush:php;toolbar:false;">for (let i = 0; i &lt; arr.length; i++) { if (arr[i] === 0) { document.getElementById(**container of the corresponding arr[i] value**).style.color = &quot;red&quot;; } else { document.getElementById(**container of the corresponding arr[i] value**).style.color = &quot;black&quot;; } }</pre> <p>现在我该如何解决这个问题?</p> <p>如果我只使用一个容器,比如 <code>in1</code>,我就可以让代码运行,所以如果我把 <code>("in1")</code> 而不是 </ p> <pre class="brush:php;toolbar:false;">(**container of the corresponding arr[i] value**)</pre></p>
P粉022140576
P粉022140576

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!