javascript 想要消除输入文本框的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18513290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:13:51  来源:igfitidea点击:

Want to disapper input textbox default value

javascripthtmltextbox

提问by AJay Garg

Please do not give a negative vote if I am wrong with asking this question. I have a TextBox like this:

如果我问这个问题有误,请不要投反对票。我有一个这样的文本框:

<input class="login-inpt" runat="server" name="loginName" type="text" value="User Name" />

It currently holds a default value. I want the default value to be cleared and replaced with a cursor when I click on the TextBox.

它目前拥有一个默认值。我希望在单击 TextBox 时清除默认值并替换为光标。

回答by kayz1

For newer browsers (IE>9):

对于较新的浏览器(IE>9):

<input type="text" id="userName" placeholder="User Name" />

For older:

对于老年人:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Placeholder support</title>
</head>
<body>
    <input type="text" id="userName" placeholder="User Name" />

    <script src="../js/vendor/jquery-1.10.2.min.js"></script>
    <script src="../js/vendor/jquery-migrate-1.2.1.js"></script>
    <script src="../js/vendor/modernizr-latest.js"></script>
    <script src="../js/placeholder.js"></script>
</body>
</html>

placeholder.js (jQuery, Modernizr)

placeholder.js (jQuery, Modernizr)

var Placeholder = (function ($, document) {

    /* Static Content */
    var _$document = $(document);

    /* Events */
    var _initPlaceholders = function () {
        _$document.find('input:text').each(function () {
            var $this = $(this);
            $this.css({ color: '#888' }).val($this.attr('placeholder'));
        });
    };

    var _placeholderEvent = function () {
        var $input = $(this), placeholderValue = $input.attr('placeholder');

        if ($input.val() === placeholderValue) {
            $input.css({ color: '#000' }).val('');
            return;
        }

        if ($input.val() === '') {
            $input.css({ color: '#888' }).val(placeholderValue);
        }
    };

    var _bindEvents = function () {
        if (!Modernizr.input.placeholder) {
            _initPlaceholders();
            _$document.on('focus blur', 'input:text', _placeholderEvent);
        }
    };

    var init = function () {
        _bindEvents();
    };

    return {
        init: init
    };
})(jQuery, document);

Placeholder.init();

回答by zigdawgydawg

HTML:

HTML:

<input type="text" value="user name" id="userName" />

With jQuery:

使用 jQuery:

$(function() {
    $('#userName').click(function() {
        $(this).val('').unbind('click');
    });
});

EDIT: Here's a demo

编辑:这是一个演示

回答by Jatin patil

You can try this

你可以试试这个

<input type="text" value="user name" onfocus="if(this.value=='user name') this.value='';">

回答by nkirkes

add the following as an attribute to your input tag:

将以下作为属性添加到您的输入标签:

onfocus="if(this.value=='A new value') this.value='';"

From: How to clear a textbox using javascript

来自:如何使用 javascript 清除文本框

回答by AJay Garg

I got the solution Thanks. input type="text" id="userName" onfocus="inputFocus(this)" onblur="inputBlur(this)"

我得到了解决方案 谢谢。input type="text" id="userName" onfocus="inputFocus(this)" onblur="inputBlur(this)"

function inputFocus(i) { if (i.value == i.defaultValue) { i.value = ""; i.style.color = "#000"; } } function inputBlur(i) { if (i.value == "") { i.value = i.defaultValue; i.style.color = "#888"; } }

回答by orustammanapov

You already got good answers but if you are newbie it might interest you how it could work without libraries.

您已经得到了很好的答案,但如果您是新手,您可能会对它如何在没有库的情况下工作感兴趣。

Here is HTML:

这是 HTML:

<input id="textBox" class="textBox phrase" type="text" value="Enter Your Text..">

you don't need to set class and id you could pick one of them

你不需要设置 class 和 id 你可以选择其中之一

Here is JavaScript:

这是 JavaScript:

var textBox = document.getElementById('textBox'), //probably the fastest method
/**************************additionally possible****************************

var textBox = document.getElementsByTagName('input')[0], //returns HTMLInputElement list (since we've got only one we chose the first one)
or
var textBox = document.querySelectorAll('textBox'), //works almost the same way as jquery does except you don't get all jquerys functionality
or
var textBox = document.getElementsByClassName('textBox')[0], //i guess its a least supported method from the list

***************************************************************************/    
    //text you want to choose for you input
    phrase = "Enter Your Text..";

function addEvent(el, ev, fn) {
    //checking method support
    //almost every browser except for ie
    if(window.addEventListener) {
        el.addEventListener(ev, fn, false);
    } 
    //ie
    else if(window.attachEvent) {
        el.attachEvent('on' + ev, fn);
        el.dettachEvent('on' + ev, fn);
    } 
}

addEvent(textBox, 'focus', function (){
    //cross browser event object
    var e = e || window.event,
        target = e.target;
    //if the text in your text box is your phrase then clean it else leave it untouched
    target.value = target.value === phrase ? "" : target.value;
});

addEvent(textBox, 'blur', function (){
    //cross browser event object
    var e = e || window.event,
        target = e.target;
    //if your text box is not empty then leave it else put your phrase in
    target.value = target.value ? target.value : phrase;
});

回答by mint

$('.login-inpt').focus(function() {
   $(this).val('');
});
$('.login-inpt').focusout(function() {
   $(this).val('User Name');
});

check out here http://jsfiddle.net/TDUKN/

在这里查看http://jsfiddle.net/TDUKN/