评论框文本区域显示默认文本,当用户聚焦它时该文本会消失

发布于 2024-10-10 02:29:50 字数 157 浏览 0 评论 0原文

嘿伙计们,我正在创建一个评论框,我想对评论框做两件事,我认为这需要 javascript,但我还不是真正使用 javascript 的专业人士。我希望评论框显示一些文本(输入您的评论),当用户单击它时,文本就会消失。另外,我想仅在用户单击评论框时显示提交按钮,你知道我可以通过什么方法来做到这一点吗?

Hey guys, I'm creating a comment box and I want to do two stuff to the comment box which I think need javascript, but I'm not really a pro using javascript yet. I want the comment box to show some text in it saying (type in your comment) and when the user clicks it, the text disappears. And also, I want to show a submit button only when the user clicks on the comment box, do you know any way I can do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剑心龙吟 2024-10-17 02:29:50

我已经建立了一个页面,展示了如何使用纯 JavaScript 执行此操作。我已经抽象了让文本输入/区域显示指导文本直到聚焦的代码。

一般来说:

  • 观察输入上的 focusblur 事件,并根据现有值/“有用”值更改值(以及可选的类)。
  • 观察评论框的 focusblur 并更改另一个元素的 CSS 类以显示和隐藏它。

如果您使用 jQuery 之类的东西,这一切都会容易一些,因为它已经包含跨浏览器代码,甚至插件处理自描述部分。如果您愿意使用 jQuery,请告诉我,我将使用它为您提供更新的答案。

编辑:好的,我添加了 jQuery 版本示例。我决定不使用插件进行自我描述,因为它只有 8 行。我还更改了该特定示例,以使用 title 属性来保存要显示的默认文本。

编辑 #2:根据 @Meke 指出的问题,我对脚本进行了小幅更新,以处理 FireFox 重新加载页面并保留默认文本的情况,然后 (以前)没有正确添加类来指示这是默认文本。

我还应该注意,我的两个示例都不包含表单的 submit 事件处理程序,用于删除碰巧仍显示默认值的输入值。您需要添加此(相当简单的代码)。

另请注意,通过实际更改输入/文本区域代码的,可能会破坏未准备好理解它的表单验证。

I have put up a page showing how to do this with plain JavaScript. I've abstracted the code for having text inputs/areas show instructional text until focused.

In general:

  • watch the focus and blur events on the input and change the value (and optionally classes) based on the existing value/'helpful' value.
  • watch the focus and blur of the comment box and change the CSS class of another element to show and hide it.

This is all a good bit easier if you use something like jQuery, since it already includes cross-browser code and even plugins to handle the self-describing part. If you're open to using jQuery, let me know and I'll give you an updated answer using that.

Edit: OK, I've added a jQuery version of the example. I decided not to use a plugin for the self-description since it is only 8 lines. I also changed that particular example to use the title attribute to hold the default text to display.

Edit #2: Based on the problem @Meke pointed out, I've made a minor update to the scripts to handle the case where FireFox reloads the page and leaves the default text in place, and then (previously) didn't properly add the class to indicate that this was default text.

I should also note that neither of my examples include a submit event handler for the form to remove the value for inputs that happen to still be showing the default value. This (rather trivial code) is something you would want to add.

Also note that by actually changing the value of the input/textarea code like this may break form validation not prepared to understand it.

不羁少年 2024-10-17 02:29:50

由于 Phrogz jQuery 版本不能很好地进行重新加载,我决定根据我所知道的情况制作自己的版本,因此,这里是:

您需要做的就是在输入中放置一个“placeholder=[text]”以获取它会显示出来(选择这个名称是因为 HTML5 使用“占位符”来实现相同的功能。

    $('input:text').each(function(){
            $(this).val($(this).attr('placeholder')).css('color','#900');
            $(this).focus(function(){
                // If value is same as 'placeholder', clear field
                if($(this).val() === $(this).attr('placeholder')){
                    $(this).val('').css('color','#000');
                } else { // Else select the whole text (one less click)
                    $(this).select();
                }
            });
            $(this).blur(function(){
                // On blur, if empty field, put placeholder back
                if ($(this).val() === ''){
                    $(this).val($(this).attr('placeholder')).css('color','#900');
                }
            });
        });

As Phrogz jQuery version doesn't play well with reloads I decided to make my own based on what I knew, as such, here it is:

All you need to do is place a "placeholder=[text]" in your input to get it to show up (This name was picked as HTML5 uses "placeholder" for the very same function.

    $('input:text').each(function(){
            $(this).val($(this).attr('placeholder')).css('color','#900');
            $(this).focus(function(){
                // If value is same as 'placeholder', clear field
                if($(this).val() === $(this).attr('placeholder')){
                    $(this).val('').css('color','#000');
                } else { // Else select the whole text (one less click)
                    $(this).select();
                }
            });
            $(this).blur(function(){
                // On blur, if empty field, put placeholder back
                if ($(this).val() === ''){
                    $(this).val($(this).attr('placeholder')).css('color','#900');
                }
            });
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文