需求:在开发的时候我们需要实现input点击后placeholder中的提示消息消失
这个时候就需要用到除了placeholder
之外的另外一种onfocus="this.placeholder=''" onblur="this.placeholder"
属性了
例如:在实现计算器的案例中:
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var oTxt1 = document.getElementById("txt1").value;
var oTxt2 = document.getElementById("txt2").value;
var oSlt = document.getElementById("select").value;
var result = "";
switch(oSlt) {
case "+":
result = parseFloat(oTxt1) + parseFloat(oTxt2);
break;
case "-":
result = parseFloat(oTxt1) - parseFloat(oTxt2);
break;
case "*":
result = parseFloat(oTxt1) * parseFloat(oTxt2);
break;
default:
result = parseFloat(oTxt1) / parseFloat(oTxt2);
}
document.getElementById("fruit").value = result;
}
</script>
</head>
<body>
<input type='text' id='txt1' placeholder="请输入第一个数" onfocus="this.placeholder=''" onblur="this.placeholder" /> </br>
<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select></br>
<input type='text' id='txt2' placeholder="请输入第二个数" onfocus="this.placeholder=''" onblur="this.placeholder" /> </br>
<input type='button' value=' = ' onclick=count() /> <!--通过 = 按钮来调用创建的函数,得到结果--> </br>
<input type='text' id='fruit' placeholder="计算结果即将展示" onfocus="this.placeholder=''" onblur="this.placeholder" /> </br>
</body>
</html>