87,910
社区成员
发帖
与我相关
我的任务
分享
document.getElementById('year').onclick = function fclear() {
document.getElementById('cl').value = "";
}
<input type="text" name="" id="year" value="" maxlength="4" size="4"/>
<input type="button" name="" id="cl" value="清除" onclick="fclear()"/>
<input type="text" name="" id="year" maxlength="4" size="4" value="2019" />
<input type="button" name="" id="cl" value="清除" onclick="fclear()"/>
<script>
function fclear() {
document.getElementById('year').value = "";
}
document.getElementById('year').onfocus = fclear;
</script>
首先 document.getElementById('year')所在的js片段,要在控件定义之后,页面已经在js片段前又了year对象的定义
其次,onfocus也好onclick也好,后边应该跟一个函数名或一个闭包函数
而本例中,应该用函数名方式定义,否则cl的onclick方法调用无法实现
再次,你得注意,input type=button、submit、reset、radio、checkbox用click事件比较好,而input type=text、password等用focus比较好
最后,你得注意,你到底要点哪个? 又要改变哪个对象的内容?cl和year分别是什么 document.getElementById("cl").onclick = function fclear() {
document.getElementById("year").value = " ";
document.getElementById("month").value = " ";
document.getElementById("day").value = " ";
}