jquery text文本框文字提示鼠标点击输入内容文字提示消失

<!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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值