解释 JavaScript 中的触摸事件
javascriptweb developmentobject oriented programming更新于 2025/8/21 17:48:17
当用户与触摸屏设备交互时,JavaScript 中的触摸事件会被触发。
以下是指针事件属性
| 事件 | 描述 |
|---|---|
| touchstart. | 当触摸点放在触摸表面上时会触发。 |
| touchmove | 当触摸点沿触摸表面移动时会触发。 |
| touchend | 当触摸点从触摸表面移除时会触发。 |
| touchcancel | 当触摸点已中断 |
以下是 JavaScript 中触摸事件的代码 −
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.result,
.sample {
font-size: 18px;
color: blueviolet;
font-weight: 500;
}
.sample {
color: red;
}
</style>
</head>
<body>
<h1>Touch events in JavaScript</h1>
<div class="sample">Here is some sample text to touch</div>
<div class="result"></div>
<h3>Touch on the above paragraph to make output in the below paragraph visible</h3>
<script>
let resEle = document.querySelector(".result");
let sampleEle = document.querySelector(".sample");
sampleEle.addEventListener("touchstart", () => {
resEle.innerHTML = "Touch start event has been triggered";
});
</script>
</body>
</html>
上述代码将产生以下输出 −
输出

关于触及段落 −

相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

