我可以使用 jquery 来清空文本区域字段或像输入框一样的 ajax 吗?
我总是在工作中使用这个片段:
<input type="text" onblur="if (this.value=='') { this.value='your email'; }" onfocus="if (this.value == 'your email') { this.value=''; }" value="your email" />
基本上,它会显示一个文本框,其中“您的电子邮件”作为值,当单击文本输入框时 - 该值变为空白..因此他们输入电子邮件..如果他们不这样做如果不输入电子邮件,它将重置回“您的电子邮件”。
我想对 textarea 执行此操作或类似操作并将其转换为 jQuery(也是 jQuery 中的上述代码)?
<textarea name="msg">Message:</textarea><br />
I always use this snippet in my work:
<input type="text" onblur="if (this.value=='') { this.value='your email'; }" onfocus="if (this.value == 'your email') { this.value=''; }" value="your email" />
Basically it will show a text box with "your email" as the value, when the clicks the text input box - the value becomes blank.. thus they type their email.. if they don't type an email it will reset back to the "your email".
I want to do this or similar with textarea and convert it to jQuery (also the above code in jQuery)?
<textarea name="msg">Message:</textarea><br />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这应该可以解决问题:
适用于输入和文本区域 - 无论您为每个区域设置什么默认值都将持续存在。按原样使用。
请参阅此处的小提琴: http://jsfiddle.net/leifparker/DvqYU/2/
(这提取并存储数据属性中的默认值)
HTML
JS
EDIT:
ANeves 提出了一种使用 HTML5 placeholder 属性的替代方案,如下所示。如果您不关心旧浏览器,您可以单独使用 占位符 HTML(它可以在本机运行,结果与上面的 JS 类似),否则,如下所示,您将需要添加一个JS后备。
在这里小提琴: http://jsfiddle.net/leifparker/DvqYU/14/
HTML
JS
This ought to do the trick:
Will work for both inputs and textareas -- Whatever default you set for each will persist. Use as is.
See Fiddle here : http://jsfiddle.net/leifparker/DvqYU/2/
(This pulls and stores the default value in a data attribute)
HTML
JS
EDIT:
An alternative brought up by ANeves, and which makes use of the HTML5 placeholder attribute is below. If you don't care about old browsers, you can use the placeholder HTML on its own (and it works natively, with results similar to the JS above), or otherwise, as below, you'll need to add a JS fallback.
Fiddle here : http://jsfiddle.net/leifparker/DvqYU/14/
HTML
JS
我认为您指的是这样的占位符:
http://andrew-jones.com/jquery- placeholder-plugin/
希望有帮助。
i think you are referring to Placeholder like this:
http://andrew-jones.com/jquery-placeholder-plugin/
Hope it helps.
您可以使用数据属性作为适用于文本区域和输入框的通用解决方案 -
HTML
jQuery
演示 - http://jsfiddle.net/3jwtA/
You could use data attributes for a generic solution that would apply to textareas and input boxes -
HTML
jQuery
Demo - http://jsfiddle.net/3jwtA/
使用本机 html 属性
title
指定掩码文本,然后应用以下脚本:html
JS
在这种情况下,您只需要为控件提供标题。
代码: http://jsfiddle.net/pJrG9/7/
Use native html attribute
title
to specify the mask text, and then apply following script:html
JS
And in this case you will need only to provide titles to the controls.
Code: http://jsfiddle.net/pJrG9/7/
这就是我会做的,希望它有帮助。
超文本标记语言
脚本
This is how I would do it, hope it helps.
HTML
SCRIPT