javascript - 使用键将点击次数推送到对象
问题描述
请问如何将点击次数与指定值的键一起推送到对象中?我在下面使用的代码。例如; 它变成了 Var Value_object = {contestant_one: 4, contestant_two: 7,...}
var total = document.querySelector(".Fixed_number");
var minusBtn = document.querySelectorAll("#minus");
var allSpan = document.getElementById('displayvalue');
var plusBtn = document.querySelectorAll("#plus");
var value_object = {};
plusBtn.forEach(function(item) {
item.onclick = function() {
if (parseInt(total.innerText) > 0) {
item.nextElementSibling.innerText = parseInt(item.nextElementSibling.innerText) + 1;
total.innerText = parseInt(total.innerText) - 1;
}
};
});
minusBtn.forEach(function(item) {
let variable;
item.onclick = function() {
if (parseInt(item.previousElementSibling.innerText) > 0) {
item.previousElementSibling.innerText = parseInt(item.previousElementSibling.innerText) - 1;
total.innerText = parseInt(total.innerText) + 1;
}
};
});
<body>
<div class="Fixed_number">10</div>
<form>
<label for=""> Contestant_One
<div id="">
<button id="plus" type="button">+</button>
<span id="displayvalue">0</span>
<button id="minus" type="button">-</button>
</div>
</label>
</form>
<form>
<label for=""> Contestant_Two
<div id="">
<button id="plus" type="button">+</button>
<span id="displayvalue">0</span>
<button id="minus" type="button">-</button>
</div>
</label>
</form>
<form>
<label for=""> Contestant_Three
<div id="">
<button id="plus" type="button">+</button>
<span id="displayvalue">0</span>
<button id="minus" type="button">-</button>
</div>
</label>
</form>
<form>
<label for="">Contestant_Four
<div id="">
<button id="plus" type="button">+</button>
<span id="displayvalue">0</span>
<button id="minus" type="button">-</button>
</div>
</label>
</form>
push the number of clicks into an object with their key from a specified value? the code I'm working with below.
<script type="text/javascript" src="app.js"></script>
</body>
解决方案
尝试这个:
const body = document.querySelector('body');
body.addEventListener('click', handleClick);
const fixedNum = document.querySelector('#Fixed_number');
let fixedNumCount = 10;
const value_object = {};
function handleClick(e) {
const targetEl = e.target.type;
const contestant = e.target.parentElement.id;
const operation = e.target.dataset.operation;
if (targetEl === 'button') {
if (value_object[contestant] === undefined) {
if (operation === 'plus') {
value_object[contestant] = 1;
fixedNumCount--;
} else {
value_object[contestant] = -1;
fixedNumCount++;
}
} else {
if (operation === 'plus') {
value_object[contestant] = ++value_object[contestant];
fixedNumCount--
} else {
value_object[contestant] = --value_object[contestant];
fixedNumCount++
}
}
document.querySelector(`div#${contestant} span`).textContent = value_object[contestant]
fixedNum.textContent = fixedNumCount;
}
}
注意:我只是将重复的 id 更改为 class,因为正如 @trincot 所提到的,您不应该在多个元素上使用相同的 id。
<body>
<div id="Fixed_number">10</div>
<form>
<label for="">Contestant_One
<div id="contestant_one">
<button data-operation="plus" type="button">+</button>
<span class="displayvalue">0</span>
<button data-operation="minus" type="button">-</button>
</div>
</label>
</form>
<form>
<label for="">Contestant_Two
<div id="contestant_two">
<button data-operation="plus" type="button">+</button>
<span class="displayvalue">0</span>
<button data-operation="minus" type="button">-</button>
</div>
</label>
</form>
<form>
<label for="">Contestant_Three
<div id="contestant_three">
<button data-operation="plus" type="button">+</button>
<span class="displayvalue">0</span>
<button data-operation="minus" type="button">-</button>
</div>
</label>
</form>
<form>
<label for="">Contestant_Four
<div id="contestant_four">
<button data-operation="plus" type="button">+</button>
<span class="displayvalue">0</span>
<button data-operation="minus" type="button">-</button>
</div>
</label>
</form>
<script src="script.js"></script>
</body>
推荐阅读
- python - 迭代 4D numpy 数组的轴数
- mysql - Mysql在两列之间选择当前时间
- android - 在 Ubuntu 18.04 上构建的 AOSP 版本 11 失败(出现段错误)
- javascript - JavaScript keydown 事件 e.shiftKey + e.keyCode 8 星号
- flutter - 渲染数据模型中的每个单独的列表项,并渲染到芯片小部件
- html - 如何在 html 或 css 中分词?
- python - 如何将模型字段分配给视图中的自定义变量,并在上下文中使用它来传递给模板?
- python - 使用python从列表中获取最小值
- json - Alamofire POST 未按预期工作。参数错误
- laravel-5 - 我想将 PostgreSQL 查询转换为 Laravel 构建器