<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>addEventListener</title>
</head>
<body>
价格:<span id="price"></span><br>
数量:<input id="count" type="text" placeholder="请输入数量"/><br>
总额:<span id="sum"></span>
<script>
let price = document.getElementById('price'),
count = document.getElementById('count'),
sum = document.getElementById('sum');
price.innerText = '100'; // string
count.value = 11; // string
// js可以把两个数字型字符串相乘,自动转化成两个数字相乘。
sum.textContent = price.innerText * count.value;
// 监视 View层的用户输入
count.addEventListener('keydown', function () {
window.setTimeout(function () {
sum.textContent = price.innerText * count.value;
}, 10);
});
</script>
</body>
</html>
js addEventListener_keydown.html
最新推荐文章于 2024-06-19 14:30:50 发布