单击后清除 TEXTAREA 值(onFocus)
我目前正在使用..
onfocus="this.value=''; return false;"
清除文本区域中的值以留下评论。但是,当您单击输入评论并输入一些内容时,如果您要单击框外以阅读页面上的其他内容,然后再次单击文本区域框返回您的评论,则会清除您当前的输入。
无论如何,是否可以在初始单击时清除文本区域,只是为了清除默认值,而不是浏览器页面会话其余部分的其余点击?
I'm currently using..
onfocus="this.value=''; return false;"
to clear the value within my textarea for leaving comments. However, when you click to enter a comment and type a few things, if you were to click outside the box to maybe read something else on the page and then return to your comment by clicking within the textarea box again, it clears your current input.
Is there anyway to clear the textarea on the initial click just to clear the default value and not the rest of the clicks for the rest of the browser page's session?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
最好将指令放入标签标记中,将其放置在文本区域上,然后在文本区域获得焦点时隐藏或显示它。这样做的原因是因为您的文本区域将填充说明,如果它们位于表单内,则可能会发送这些说明。
但是,解决你的问题,你所要做的就是:
It's better to put instructions into a label tag, position it over the textarea, and then hide it or show it if the textarea is focused. The reason for this is because your textearea will be populated with instructions and these might get sent if they are inside a form.
But, solve your problem, all you have to do is:
我认为将逻辑与内容分开是一个好习惯。因此,使用 jQuery(我们都使用 jQuery,是吗?),您可以这样做:
这将迭代站点上的所有文本区域,并将有关其清晰状态的布尔值存储在单独的数据绑定中。 onclick 事件也是针对每个文本区域单独处理的,因此您可以在单个页面上拥有多个文本区域/文本区域清除,并且不需要 Javascript 散布您的 html。
I think it's a good habit to seperate the logic from the content. So, using jQuery (we all use jQuery, do we?) you could do:
This will iterate over all the textareas on the site and store a boolean value about their clear state in a seperate data binding. The onclick event is also handled separately for each textarea, so you can have multiple textarea / textarea clearings on a single page and no need for Javascript splattering your html.
这将有助于
onfocus html 属性
this will help
and onfocus html attribute
来源:http://snipplr.com/view/2206/清除表单字段第一焦点/
Source: http://snipplr.com/view/2206/clear-form-field-on-first-focus/
我知道这已经晚了,但对于遇到此问题的其他人,我相信最简单的答案是
onfocus="this.value=''; this.onfocus='';"
应该完全按照您的要求进行无需存储/检查任何变量。
I know this is late, but to anyone else having this problem I believe the simplest answer is
onfocus="this.value=''; this.onfocus='';"
should do exactly what you want without having to store/check any variables.
相反,请使用 HTML5 的
placeholder
并使用适用于旧版浏览器的 jquery 插件。此处运行示例:
http://jsfiddle.net/dream2unite/xq2Cx/
必须附带以下脚本:
当您输入电子邮件,您的文本(占位符)将会消失,只有当您删除电子邮件时,文本(占位符)才会重新出现。
最后,一个明显很好的占位符 jquery 插件示例是:
http://mathiasbynens.be/demo/placeholder
Instead, use HTML5's
placeholder
and use a jquery plugin for older browsers.Running example here:
http://jsfiddle.net/dream2unite/xq2Cx/
must be accompanied by the following script:
When you enter an email your text (placeholder) will disappear, only when you delete your email will the text (placeholder) reappear.
And finally, an obviously good example of a jquery plugin for placeholders is this one:
http://mathiasbynens.be/demo/placeholder
您可以使用全局页面变量。
元素上方的
script
元素中...在稍后
You could use a global page var.
In a
script
element above the elementLater...
所以我认为您有一个预填充值,例如“在此处输入您的文本”?
尝试添加此 javascript:
然后,您的输入应该是:
在这里输入您的文字
这应该可以帮助您开始...
So I take it you have a prefilled value such as "Enter your text here"?
Try adding this javascript:
Then, your input should be:
Enter your text here
This should get you started...
在 javascript 中声明一个 false 变量
clearedOnce
。在你的焦点中,检查这个变量。如果为 false,则将其设置为 true 并清除文本区域。以后的所有点击都不会执行任何操作。代码 :Declare a variable
clearedOnce
in javascript that is false. In your onfocus, check this variable. If it is false, then set it to true and clear your textarea. All later clicks will do nothing. Code :