<input class="keyword" type="text" value="请输入" onfocus="first()" onblur="ff()"> <script> var ky; function first() { // console.log("this is first1"); //焦点触发 ky = document.getElementsByClassName("keyword")[0]; if(ky.value ==="请输入"){ ky.value=""; } } function ff() { // console.log("this is first2"); //失去焦点触发 if (ky.value.trim().length === 0) { ky.value = "请输入"; } }
<script>
//要是script放在body上面那么就要在加上这个
window.onload=function () {
}
</script>