简体   繁体   English

Javascript可以将背景颜色更改为某些<div>

[英]Javascript can change background color to certain <div>

I have the following HTML code in a table: 我在表中有以下HTML代码:

 $(function() { $(".numbers-row").append('<div class="inc button">+</div><div class="dec button">-</div>'); $(".button").on("click", function() { var $button = $(this); var oldValue = $button.parent().find("input").val(); var divut = document.getElementById('french-hens'); if ($button.text() == "+") { var newVal = parseFloat(oldValue) + 1; } else { // Don't allow decrementing below zero if (oldValue > 0) { var newVal = parseFloat(oldValue) - 1; } else { newVal = 0; document.getElementById("french-hens").style.background = 'orange'; } } $button.parent().find("input").val(newVal); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <td> <div class="numbers-row"> <input type="text" name="french-hens" id="french-hens" value="45"> </td> <td> <div class="numbers-row"> <input type="text" name="french-hens" id="french-hens" value="30"> </td> <td> <div class="numbers-row"> <input type="text" name="french-hens" id="french-hens" value="`0"> </td> <td> <div class="numbers-row"> <input type="text" name="french-hens" id="french-hens" value="`10"> </td> 

What I want to accomplish when the value of that div reach 0, the background color is changed. 当该div的值达到0时,我想完成的事情是改变了背景色。

With my Javascript code I managed to change only the first div. 使用我的Javascript代码,我仅更改了第一个div。

Any ideas how to start with orange all the divs that already have value 0, and when I decrease the value to 0 to change the color automatically in orange. 任何想法如何以橙色开始,所有已经具有值0的div,以及当我将该值减小为0时自动以橙色更改颜色。 Thank in advance 预先感谢

You're running document.getElementById('french-hens') which only returns one element (per the name of the method), even though you have multiple elements with an id of 'french-hens'. 您正在运行document.getElementById('french-hens') ,即使您有ID为'french-hens'的多个元素,它也仅返回一个元素(按方法的名称)。

Instead, try using document.getElementsByName('french-hens') . 相反,请尝试使用document.getElementsByName('french-hens') This will give you an array that you'll have to loop through and set the color to orange on each individual element of the array. 这将为您提供一个数组,您必须循环遍历该数组,并在该数组的每个元素上将颜色设置为橙色。

Also, the point of an id is for it to be unique on the page. 另外,ID的要点是它在页面上是唯一的。 Having multiple elements with an id of 'french-hens' violates that. 具有多个id为“ french-hens”的元素违反了该规定。

Here's an example: 这是一个例子:

 var el1 = document.createElement('div'); var el2 = document.createElement('div'); el1.setAttribute('id', 'test'); el2.setAttribute('id', 'test'); el1.setAttribute('name', 'test'); el2.setAttribute('name', 'test'); document.body.appendChild(el1); document.body.appendChild(el2); var byId = document.getElementById('test'); var byName = document.getElementsByName('test'); console.log('document.getElementById:\\n', byId); console.log('document.getElementsByName:\\n', byName); 

I agree with Xeraqu's points about the use of Ids. 我同意Xeraqu关于使用Ids的观点。

However, to get what I think you want, the only thing you needed to do was reuse the principles behind some of the code you already used: 但是,要获得我认为想要的东西,您唯一需要做的就是重用一些已经使用的代码背后的原理:

$button.parent().find("input").val(newVal);

changing ".val" to ".css": 将“ .val”更改为“ .css”:

$button.parent().find("input").css("background-color","orange");

see this fiddle: https://jsfiddle.net/063mawvq/5/ 看到这个小提琴: https : //jsfiddle.net/063mawvq/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM