本文介绍了onFocus和onBlur的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
出于某种原因,onblur和onfocus属性不起作用。我是否正确定义它们?
For some reason the onblur and onfocus properties don't work. Am I defining them right?
var replyTxt = document.createElement("input");
replyTxt.id="replyTxt";
replyTxt.type="text";
replyTxt.value="Write a reply";
replyTxt.onfocus = function(){if(replyTxt.value==replyTxt.defaultValue) replyTxt.value='';};
replyTxt.onblur= function(){if(replyTxt.value=='') replyTxt.value=replyTxt.defaultValue;};
我也试过把function(){if(replyTxt.value) == replyTxt.defaultValue)replyTxt.value ='';}
in quotations
I have also tried putting "function(){if(replyTxt.value==replyTxt.defaultValue) replyTxt.value='';}"
in quotations
推荐答案
事件有效 - 所以你的逻辑可能有问题......
The events work - so there may be a problem with your logic...
if(replyTxt.value==replyTxt.defaultValue) replyTxt.value='';
你在哪里设置replyTxt.defaultValue?在你的例子中,你没有 - 所以逻辑永远不会触发。试试这个......
Where do you set replyTxt.defaultValue? In your example, you don't - so the logic will never fire. Try this...
var replyTxtDefaultText = "Write a reply";
var replyTxt = document.createElement("input");
replyTxt.id="replyTxt";
replyTxt.type="text";
replyTxt.value=replyTxtDefaultText;
replyTxt.onfocus = function(){if(replyTxt.value==replyTxtDefaultText) replyTxt.value='';};
replyTxt.onblur= function(){if(replyTxt.value=='') replyTxt.value=replyTxtDefaultText;};
这篇关于onFocus和onBlur的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!