单击后清除 TEXTAREA 值(onFocus)

发布于 2024-10-18 07:17:43 字数 235 浏览 7 评论 0原文

我目前正在使用..

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 技术交流群。

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

发布评论

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

评论(9

猫瑾少女 2024-10-25 07:17:43

最好将指令放入标签标记中,将其放置在文本区域上,然后在文本区域获得焦点时隐藏或显示它。这样做的原因是因为您的文本区域将填充说明,如果它们位于表单内,则可能会发送这些说明。

但是,解决你的问题,你所要做的就是:

onfocus="if(this.value== "your initial text"){this.value=''}; return false;"

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:

onfocus="if(this.value== "your initial text"){this.value=''}; return false;"
空城仅有旧梦在 2024-10-25 07:17:43

我认为将逻辑与内容分开是一个好习惯。因此,使用 jQuery(我们都使用 jQuery,是吗?),您可以这样做:

$(document).ready(function() {
    var _bCleared = false;
    $('form textarea').each(function()
    {
        var _oSelf = $(this);

        _oSelf.data('bCleared', false);
        _oSelf.click(function()
        {
            if (!_oSelf.data('bCleared'))
            {
                _oSelf.val('');
                _oSelf.data('bCleared', true);
            }
        });
    });
});

这将迭代站点上的所有文本区域,并将有关其清晰状态的布尔值存储在单独的数据绑定中。 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:

$(document).ready(function() {
    var _bCleared = false;
    $('form textarea').each(function()
    {
        var _oSelf = $(this);

        _oSelf.data('bCleared', false);
        _oSelf.click(function()
        {
            if (!_oSelf.data('bCleared'))
            {
                _oSelf.val('');
                _oSelf.data('bCleared', true);
            }
        });
    });
});

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.

遗弃M 2024-10-25 07:17:43

这将有助于

function clearTA {
   if(this.value != "type here") {
      this.value = ''; 
      return false;
   }
}

onfocus html 属性

...onfocus="clearTA()"..

this will help

function clearTA {
   if(this.value != "type here") {
      this.value = ''; 
      return false;
   }
}

and onfocus html attribute

...onfocus="clearTA()"..
暮年 2024-10-25 07:17:43
<script type="text/javascript">
function clearOnInitialFocus ( fieldName ) {
   var clearedOnce = false;
   document.getElementById( fieldName ).onfocus = (function () {
    if (clearedOnce == false) {
      this.value = '';
      clearedOnce = true;
    }
  })
}
window.onload = function() { clearOnInitialFocus('myfield');
</script>

来源:http://snipplr.com/view/2206/清除表单字段第一焦点/

<script type="text/javascript">
function clearOnInitialFocus ( fieldName ) {
   var clearedOnce = false;
   document.getElementById( fieldName ).onfocus = (function () {
    if (clearedOnce == false) {
      this.value = '';
      clearedOnce = true;
    }
  })
}
window.onload = function() { clearOnInitialFocus('myfield');
</script>

Source: http://snipplr.com/view/2206/clear-form-field-on-first-focus/

已下线请稍等 2024-10-25 07:17:43

我知道这已经晚了,但对于遇到此问题的其他人,我相信最简单的答案是

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.

虐人心 2024-10-25 07:17:43

相反,请使用 HTML5placeholder 并使用适用于旧版浏览器的 jquery 插件。

此处运行示例:
http://jsfiddle.net/dream2unite/xq2Cx/

<input type="text" name="email" id="email" placeholder="[email protected]">

必须附带以下脚本:

$('input[placeholder], textarea[placeholder]').placeholder();

当您输入电子邮件,您的文本(占位符)将会消失,只有当您删除电子邮件时,文本(占位符)才会重新出现。

最后,一个明显很好的占位符 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/

<input type="text" name="email" id="email" placeholder="[email protected]">

must be accompanied by the following script:

$('input[placeholder], textarea[placeholder]').placeholder();

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

寒尘 2024-10-25 07:17:43

您可以使用全局页面变量。

元素上方的 script 元素中...

var tracker = {};

在稍后

<textarea name="a" onfocus="if(!tracker[this.name]) { tracker[this.name]=true; this.value=''; } return false;">Placeholder text</textarea>

You could use a global page var.

In a script element above the element

var tracker = {};

Later...

<textarea name="a" onfocus="if(!tracker[this.name]) { tracker[this.name]=true; this.value=''; } return false;">Placeholder text</textarea>
无需解释 2024-10-25 07:17:43

所以我认为您有一个预填充值,例如“在此处输入您的文本”?
尝试添加此 javascript:

    function clearText(textarea,prefilled){
        if(textarea.value==prefilled)
            textarea.value="";
        return false;
    }

然后,您的输入应该是:
在这里输入您的文字

这应该可以帮助您开始...

So I take it you have a prefilled value such as "Enter your text here"?
Try adding this javascript:

    function clearText(textarea,prefilled){
        if(textarea.value==prefilled)
            textarea.value="";
        return false;
    }

Then, your input should be:
Enter your text here

This should get you started...

柠檬心 2024-10-25 07:17:43

在 javascript 中声明一个 false 变量 clearedOnce。在你的焦点中,检查这个变量。如果为 false,则将其设置为 true 并清除文本区域。以后的所有点击都不会执行任何操作。代码 :

onfocus='if (!clearedOnce) { this.value = ''; clearedOnce = true;} return false;'

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 :

onfocus='if (!clearedOnce) { this.value = ''; clearedOnce = true;} return false;'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文