通過按鈕控制div文本的編輯

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
    <title>demo</title>
    <style type="text/css">

        .edit_container {
            width: 500px;
            height: 30px;
            background: #599fc2;
            padding: 3px;
            border-radius: 3px;
            line-height: 30px;
        }

        .btn {
            margin-top: 20px;
            width: 80px;
            height: 20px;
            background: #599fc2;
            padding: 3px;
            border-radius: 3px;
            line-height: 20px;
            text-align: center;
            cursor: pointer;
        }

        .enable_edit {
            border: 1px dashed #1248dd;
        }
    </style>
</head>

<body>
    <p>點擊按鈕開啓編輯,失去焦點後關閉編輯功能</p>
    <div class="edit_container"></div>
    <div class="btn">
        開啓編輯
    </div>

    <script src="jquery.min.js"></script>
    <script>
        $(() => {
            $('.btn').click(() => {
                //增加可編輯的屬性
                $('.edit_container').attr('contenteditable', true)
                $('.edit_container').focus()
                //增加編輯時效果
                $('.edit_container').addClass('enable_edit')
            });
			//失去焦點時移除掉
            $('.edit_container').blur(() => {
                $('.edit_container').removeAttr('contenteditable')
                $('.edit_container').removeClass('enable_edit')
            });
        });
    </script>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章