以下是jQuery与原生js获取文本框默认值的方法,直接粘贴保存为HTML文档可用。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery与原生js获取文本框默认值的方法</title>
<script src="http://apps.bdimg.com/libs/jquery/1.11.3/jquery.js"></script>
<style type="text/css">
div,span{
width: 140px;
height: 140px;
margin: 20px;
background: #9999CC;
border: #000 1px solid;
float:left;
font-size: 17px;
font-family:Roman;
}
div.mini{
width: 30px;
height: 30px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
</style>
<!--引入jquery的js库-->
</head>
<body>
<input type="text" value="用户邮箱/手机号/用户名" id="b1"/><br>
<input type="password" value="" id="b2"/><br>
<input type="button" value="登陆" id="b3"/>
<br>
</body>
<script language="JavaScript">
$("#b1").focus(function () {
console.log("用JQ方法($(\"#b1\")[0].defaultValue)获取#b1的默认值:\n" + $("#b1")[0].defaultValue);
console.log("用JS方法(this.defaultValue)获取#b1默认值:\n" + this.defaultValue);
})
$("#b2").focus(function () {
$("#b2").val("");
})
</script>
</html>
达维营-前端网