【发布时间】:2013-04-10 03:35:50
【问题描述】:
我正在尝试使用 jquery 为 textarea 实现基本的占位符逻辑。但是有一个小问题我无法弄清楚如何解决它。这是html;
<textarea class="commentInput" rows="1" cols="50" title="Say something...">
Say something...
</textarea>
而 javascript 是 ;
$('.commentInput').live('focus', function() {
if ($(this).val() == $(this).attr('title')) {
$(this).val('');
}
});
$('.commentInput').live('blur', function() {
if ($(this).val() == '' && $(this).attr('title') != '') {
$(this).val($(this).attr('title'));
$(this).attr('rows', 1);
}
});
一切正常,但有一种情况坏了。假设我在 textarea 中键入“Say something...”,然后单击 html 中的其他位置,然后当我重新回到 textarea 时,它会清除文本,因为jquery 焦点事件的值与文本区域的值和文本区域的标题匹配..
我该如何解决它或任何其他想法? 谢谢。
【问题讨论】:
-
无法理解您面临的问题
-
你想为 IE 做对吗?有很多插件。例如:code.google.com/p/jquery-placeholder-js 或 github.com/mathiasbynens/Placeholder-jQuery-Plugin
-
我想你正在寻找这样的东西:stackoverflow.com/questions/5059732/…
-
@ScottE 我检查了你的链接,但你的解决方案有同样的问题?
-
@tylerdurden - 不 - 它正在检查 defaultValue,这是在页面加载时存储在 textarea 中的文本。