使用CSS和JavaScript在网页中隐藏光标
给定一个HTML文档,任务是使用CSS和JavaScript从给定的元素中隐藏光标。
方法:
- 首先,选择需要隐藏光标的元素。
- 在类上添加CSS样式 cursor: none 。
- 将类名(CSS样式cursor: none的类名)添加到需要隐藏光标的特定元素上。
示例1: 此示例将光标从div
元素中隐藏。
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Hide the cursor in a webpage
using CSS and JavaScript
</title>
<style>
body {
text-align: center;
}
#GFG_DIV {
background: green;
height: 100px;
width: 200px;
margin: auto;
color: white;
}
/* CSS style to hide cursor element */
.newClass {
cursor: none;
}
</style>
</head>
<body>
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h3>
Click on Button to Hide the Cursor from DIV
</h3>
<div id="GFG_DIV"></div>
<br>
<button onClick="GFG_Fun()">
Click Here
</button>
<p id="GFG"></p>
<!-- Script to hide cursor element -->
<script>
let elm = document.getElementById('GFG');
let div = document.getElementById('GFG_DIV');
/* Function to add class name to hide
cursor element */
function GFG_Fun() {
div.classList.add("newClass");
down.innerHTML = "Cursor is removed from DIV!";
}
</script>
</body>
</html>
输出:
示例2: 此示例将隐藏网络页面的<body>
中的光标。
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Hide the cursor in a webpage
using CSS and JavaScript
</title>
<style>
body {
text-align: center;
}
#GFG_DIV {
background: green;
height: 100px;
width: 200px;
margin: 0 auto;
color: white;
}
/* CSS style to hide cursor element */
.newClass {
cursor: none;
}
</style>
</head>
<body id="body">
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h3>
Click on Button to Hide Cursor from Body
</h3>
<div id="GFG_DIV"></div>
<br>
<button onClick="GFG_Fun()">
Click Here
</button>
<p id="GFG"></p>
<!-- Script to hide cursor element -->
<script>
let elm = document.getElementById('GFG');
let body = document.getElementById('body');
/* Function to add class name to
hide cursor element */
function GFG_Fun() {
body.classList.add("newClass");
elm.innerHTML = "Cursor is removed from body!";
}
</script>
</body>
</html>
输出: