<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 = "请输入"; } }
原文:https://www.cnblogs.com/TKOPython/p/12823087.html