<jQuery> <方法> 十三. val()方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="button" value="Search" id="btn">
<input type="text" value="香烟" id="txt">
<script src="jquery-3.2.1.js"></script>
<script>
    $(function () {
        // 获取值
        console.log($("input").val());

        // 设置新值
        $("#btn").val("搜索");

        // 也可用 focus, blur事件
        // 得到焦点, 清空内容
        $("#txt").focusin(function () {
            $(this).val("");
        });

        // 失去焦点, 设置默认内容
        $("#txt").focusout(function () {
            var str = $(this).val();
            if (str.trim() == 0) {
                $(this).val("香烟");
            }
        })
    });
</script>
</body>
</html>
原文地址:https://www.cnblogs.com/ZeroHour/p/8274714.html