onFocus 和 onBlur
由于某种原因,onblur
和 onfocus
属性不起作用。我对它们的定义正确吗?
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='';}"
放在引号中
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;};
I have also tried putting "function(){if(replyTxt.value==replyTxt.defaultValue) replyTxt.value='';}"
in quotations
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
事件有效 - 所以你的逻辑可能有问题...
你在哪里设置replyTxt.defaultValue?在你的例子中,你没有 - 所以逻辑永远不会触发。试试这个...
The events work - so there may be a problem with your logic...
Where do you set replyTxt.defaultValue? In your example, you don't - so the logic will never fire. Try this...
只是一个提示:
尝试在事件函数中将
replyTxt
替换为this
。Just a hint:
try replacing
replyTxt
withthis
in your event functions.尝试
setAttribute
。有关函数的更多信息,请查看此链接try
setAttribute
. more information about function take a look this link并确保将其附加到某些东西上。
And make sure you append it to something.
实际上,我正在做一个类似的家庭作业问题,我发现最好是在鼠标落在字段上时立即清除字段,而不是在保持不变的情况下进行比较然后清除它。
Actually I was working on a similar problem as a homework and I found out that is better to clear the field as soon as the mouse lands on the field rather than making a comparison if left untouched and then clear it.