输入时的默认文本

发布于 2024-07-26 01:32:10 字数 38 浏览 9 评论 0原文

如何在输入字段上设置空白默认文本并在元素处于活动状态时清除它。

How to set blank default text on input field and clear it when element is active.

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

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

发布评论

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

评论(9

坐在坟头思考人生 2024-08-02 01:32:10

在现代浏览器中,您可以在字段上设置 placeholder 属性来设置其默认文本。

<input type="text" placeholder="Type some text" id="myField" />

但是,在较旧的浏览器中,您可以使用 JavaScript 来捕获焦点和模糊事件:

var addEvent = function(elem, type, fn) { // Simple utility for cross-browser event handling
    if (elem.addEventListener) elem.addEventListener(type, fn, false);
    else if (elem.attachEvent) elem.attachEvent('on' + type, fn);
},
textField = document.getElementById('myField'),
placeholder = 'Type some text'; // The placeholder text

addEvent(textField, 'focus', function() {
    if (this.value === placeholder) this.value = '';
});
addEvent(textField, 'blur', function() {
    if (this.value === '') this.value = placeholder;
});

演示:http://jsbin.com/utecu

In modern browsers, you may set the placeholder attribute on a field to set its default text.

<input type="text" placeholder="Type some text" id="myField" />

However, in older browsers, you may use JavaScript to capture the focus and blur events:

var addEvent = function(elem, type, fn) { // Simple utility for cross-browser event handling
    if (elem.addEventListener) elem.addEventListener(type, fn, false);
    else if (elem.attachEvent) elem.attachEvent('on' + type, fn);
},
textField = document.getElementById('myField'),
placeholder = 'Type some text'; // The placeholder text

addEvent(textField, 'focus', function() {
    if (this.value === placeholder) this.value = '';
});
addEvent(textField, 'blur', function() {
    if (this.value === '') this.value = placeholder;
});

Demo: http://jsbin.com/utecu

空心↖ 2024-08-02 01:32:10

使用 onFocusonBlur 事件可以让您实现这一点,即:

onfocus="if(this.value=='EGTEXT')this.value=''" 

完整

onblur="if(this.value=='')this.value='EGTEXT'"

的示例如下:

<input name="example" type="text" id="example" size="50" value="EGTEXT" onfocus="if(this.value=='EGTEXT')this.value=''" onblur="if(this.value=='')this.value='EGTEXT'" />

Using the onFocus and onBlur events allows you to achieve this, I.e.:

onfocus="if(this.value=='EGTEXT')this.value=''" 

and

onblur="if(this.value=='')this.value='EGTEXT'"

The full example is as follows:

<input name="example" type="text" id="example" size="50" value="EGTEXT" onfocus="if(this.value=='EGTEXT')this.value=''" onblur="if(this.value=='')this.value='EGTEXT'" />
随梦而飞# 2024-08-02 01:32:10

或者简单地说,

<input name="example" type="text" id="example" value="Something" onfocus="value=''" />

一旦清除该框,这将不会回发默认文本,但也将允许用户清除该框并在自动完成脚本的情况下查看所有结果。

Or simply

<input name="example" type="text" id="example" value="Something" onfocus="value=''" />

This will not post back the default text once the box is cleared but also will allow the user to clear the box and see all results in the case of an autocomplete script.

要走干脆点 2024-08-02 01:32:10

声明非活动和活动状态的样式:

.active {
    color: black;
}

.inactive {
    color: #909090;
}

添加 Javascript 来处理状态更改:

function toggleText(el)
{
    var v = el.value;

   //Remove text to allow editing
    if(v=="Default text") {
        el.value = "";
        el.className = "active";
    }
    else {
         //Remove whitespace
        if(v.indexOf(" ")!=-1) {
            split = v.split(" ").join("");
            v = split;
        }

          //Change to inactive state
        if(v=="") {
            el.value = "Default text";
            el.className = "inactive";
        }
    }
}

添加输入框,设置默认值、inactive 类集和指向 toggleText 的 Javascript 处理程序() 函数(您可以使用 事件监听器 来执行此操作如果你希望)

<input type="text" value="Default text" class="inactive" onFocus="toggleText(this);" onBlur="toggleText(this);">

Declare styles for inactive and active states:

.active {
    color: black;
}

.inactive {
    color: #909090;
}

Add the Javascript to handle the changing of state:

function toggleText(el)
{
    var v = el.value;

   //Remove text to allow editing
    if(v=="Default text") {
        el.value = "";
        el.className = "active";
    }
    else {
         //Remove whitespace
        if(v.indexOf(" ")!=-1) {
            split = v.split(" ").join("");
            v = split;
        }

          //Change to inactive state
        if(v=="") {
            el.value = "Default text";
            el.className = "inactive";
        }
    }
}

Add your input box, with the default value set, the inactive class set and Javascript handlers pointing to the toggleText() function (you could use event listeners to do this if you wish)

<input type="text" value="Default text" class="inactive" onFocus="toggleText(this);" onBlur="toggleText(this);">
一场信仰旅途 2024-08-02 01:32:10

从可用性的角度来看,输入组件中的文本应仅保留用于用户输入目的。 如果用户不改变,输入中可能的默认值应该是有效的。

如果占位符文本旨在提示如何填充输入,则最好将其放置在输入附近,以便在填充输入时也可以看到它。 此外,在文本组件内使用占位符文本可能会导致问题,例如盲文设备。

如果使用占位符文本,无论可用性准则如何,都应该确保它以一种不引人注目的方式完成,以便它可以在没有 JavaScript 的情况下或在关闭 js 时与用户代理一起使用。

From a usability point of view the text in the input component should be preserved only for user's input purposes. The possible default value in the input should be valid if left untouched by the user.

If the placeholder text is meant to be a hint for how to fill the input, it is better to be blaced near the input where it can be seen also when the input has been filled. Moreover, using a placeholder text inside text components can cause troubles e.g. with braille devices.

If a placeholder text is used, regardless of usability guidelines, one should make sure that it is done in an unobtrusive way so that it works with user agents without javascript or when js is turned off.

暮年慕年 2024-08-02 01:32:10

我所做的是为现代浏览器添加一个占位符属性:

<input id='search' placeholder='Search' />

然后,我使用 JQuery 为旧浏览器做了后备:

var placeholder = $('search').attr('placeholder');
//Set the value
$('search').val(placeholder);

//On focus (when user clicks into the input)
$('search').focus(function() {
   if ($(this).val() == placeholder)
     $(this).val('');
});

//If they focus out of the box (tab or click out)
$('search').blur(function() {
   if ($(this).val() == '')
     $(this).val(placeholder);
});

这对我有用。

What I did is put a placeholder attribute for modern browsers:

<input id='search' placeholder='Search' />

Then, I made a fallback for older browsers using JQuery:

var placeholder = $('search').attr('placeholder');
//Set the value
$('search').val(placeholder);

//On focus (when user clicks into the input)
$('search').focus(function() {
   if ($(this).val() == placeholder)
     $(this).val('');
});

//If they focus out of the box (tab or click out)
$('search').blur(function() {
   if ($(this).val() == '')
     $(this).val(placeholder);
});

This works for me.

沫离伤花 2024-08-02 01:32:10

你可以使用这个插件(我是合著者)

https://github.com/tanin47/jquery .default_text

它克隆一个输入字段并将其放在那里。

它适用于 IE、Firefox、Chrome 甚至 iPhone Safari,后者存在着名的焦点问题。

这样您就不必担心在提交之前清除输入字段。

或者

如果您只想使用 HTML5,您可以在输入字段上使用属性“占位符”

You can use this plugin (I'm an co-author)

https://github.com/tanin47/jquery.default_text

It clones an input field and put it there.

It works on IE, Firefox, Chrome and even iPhone Safari, which has the famous focus problem.

This way you do not have to be worried about clearing input field before submitting.

OR

If you want to HTML5 only, you can just use attribute "placeholder" on input field

情丝乱 2024-08-02 01:32:10

您可以使用占位符属性。
NP。
检查http://www.w3schools.com/tags/att_input_placeholder.asp

You can use placeholder attribute.
np. <input type="text" name="fname" placeholder="First name">
check http://www.w3schools.com/tags/att_input_placeholder.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文