JavaScript -- Web API 学習ノート (2) 操作要素インスタンス

備考:スタイル属性の操作

① element.style//inline スタイル操作、スタイルが少なくシンプルな機能の場合に適しています

② element.className //クラス名のスタイル操作。多くのスタイルや複雑な機能がある場合に適しています。また、className は要素のクラス名を直接変更し、元のクラス名を上書きします. 保持したい場合は、複数のクラス名セレクターを使用できます.

JS でのスタイル スタイル操作の変更、生成されたスタイルはインライン、CSS の重みは低い

1. クリックしてパスワードを表示

パスワードを入力する:

写真をクリックするとパスワードが表示されます。 

コード:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            position: relative;
            width: 400px;
            border-bottom:1px solid #ccc;
            margin:100px auto;
        }
        .box input{
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }
        .box img{
            width: 24px;
            position: absolute;
            top: 9px;
            right:2px;
            width:24px;
        }
    </style>

</head>
<body>
    <div class="box">
        <label for="">
            <img src="D:\image\img1.jpg" alt="" id="eye">
        </label>
        <input type="password" id="pwd">
    </div>
    <script>
        var eye = document.getElementById('eye');
        var pwd = document.getElementById('pwd');
        var flag = 0;
        eye.onclick = function(){
            if(flag == 0){
                pwd.type ='text';
                eye.src = 'D:/image/img2.jpg'
                flag = 1;
            }else{
                pwd.type = 'password';
                eye.src = 'D:/image/img1.jpg'
                flag = 0;
            }

        }
        //按钮点多次不同状态的情况,用flag记录状态

    </script>
</body>
</html>

2. 例 - テキスト ボックスの内容を表示および非表示にする

①入力内容がない場合、薄い灰色の「おすすめコンテンツ」が表示されます。

②テキストを入力し、暗いテキストを表示する

③他の位置をクリックすると色が薄いグレーになります

④コンテンツがない場合は薄い灰色の「おすすめコンテンツ」に戻ります

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        input{
            color: #999;
        }
    </style>
</head>
<body>
    <input type="text" value="推荐的内容">
    <script>
        var text=document.querySelector('input');
        // 获得焦点事件
        text.onfocus = function(){
            console.log('得到了焦点');
            if(this.value === '推荐的内容'){
                this.value = '';
            }
            this.style.color = '#333';
        }
        text.onblur = function(){
            console.log('失去了焦点');
            if(this.value === ''){
                this.value = '推荐的内容';
            }
            this.style.color = '#999';
        }
        
    </script>
</body>
</html>

 3. 例 - パスワードボックス検証情報

判定イベントは、フォームがフォーカスを失うことです

入力長が 6 ~ 18 桁の場合、長さは適切であると判断されます。それ以外の場合、長さは要件を満たしていません。

以前の表示パスワードの内容と組み合わせる

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>密码框验证信息</title>
    <style>
        div{
            width: 600px;
            margin: 100px auto;
        }

        .message{
            display: inline-block;
            font-size: 12px;
            color: #999;
            background: url(D:/image/img1.jpg) no-repeat right center;
            padding-left: 20px;
        }
        .wrong{
            color: red;
            background-image: url(D:/image/img2.jpg);
        }
        .right{
            color: green;
            background-image: url(D:/image/img3.jpg);
        }
        .register img{
            width: 24px;
             /* position: absolute;  */
            top: 9px;
            right:2px;
            width:24px;
        }
        .register input{
            width: 150px;
            height: 30px;
            border: 1px;
            outline: none;
            border-bottom:1px solid #ccc;
        }
    </style>
</head>
<body>
    <div class="register">
        <input type="password" class="ipt" name="" id="">
        <img src="D:\image\img1.jpg" alt="" id="eye">
        <p class="message">请输入6-16位密码</p>
    </div>
    <script>
        var ipt = document.querySelector('.ipt');
        var message = document.querySelector('.message');

        ipt.onblur = function(){
            if(this.value.length<6 || this.value.length>18 ){
                console.log("错误");
                message.className = 'message wrong';
                message.innerHTML = '您输入的位数不满足要求';
            }
            else{
                message.className = 'message right';
                message.innerHTML = '您输入的密码满足位数要求'
            }
        }
        var eye = document.getElementById('eye');
        var message = document.querySelector('.message')
        var flag = 0;
        eye.onclick = function(){
            if(flag == 0){
                ipt.type ='text';
                eye.src = 'D:/image/img2.jpg'
                flag = 1;
            }else{
                ipt.type = 'password';
                eye.src = 'D:/image/img1.jpg'
                flag = 0;
            }
        }
    </script>
</body>
</html>

 

おすすめ

転載: blog.csdn.net/weixin_44400887/article/details/124019798