<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Text Input with Placeholder</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 50px;
background-color: #f5f5f5;
}
.container {
max-width: 500px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
h1 {
color: #333;
text-align: center;
}
.input-container {
margin: 20px 0;
}
.text-input {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
box-sizing: border-box;
}
.text-input:focus {
outline: none;
border-color: #4285f4;
box-shadow: 0 0 5px rgba(66, 133, 244, 0.3);
}
.placeholder-style {
color: #999;
}
.footer {
text-align: center;
margin-top: 30px;
font-size: 12px;
color: #777;
}
a {
color: #4285f4;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<h1>Text Input Demo</h1>
<div class="input-container">
<input type="text" id="textInput" class="text-input" value="Enter your text here">
</div>
<p>Try clicking on the input field above to see the placeholder text disappear.</p>
</div>
<div class="footer">
</div>
<script>
$(document).ready(function() {
// Set initial placeholder style
$('#textInput').addClass('placeholder-style');
// Handle focus event
$('#textInput').focus(function() {
if ($(this).hasClass('placeholder-style')) {
$(this).val('');
$(this).removeClass('placeholder-style');
}
});
// Handle blur event
$('#textInput').blur(function() {
if ($(this).val() === '') {
$(this).val('Enter your text here');
$(this).addClass('placeholder-style');
}
});
});
</script>
</body>
</html>
jquery text文本框文字提示鼠标点击输入内容文字提示消失
于 2025-04-28 14:30:36 首次发布