Javascript 要取消输入文本框的默认值吗

Javascript 要取消输入文本框的默认值吗,javascript,html,textbox,Javascript,Html,Textbox,如果我问错了这个问题,请不要投反对票。我有一个这样的文本框: <input class="login-inpt" runat="server" name="loginName" type="text" value="User Name" /> 它当前持有一个默认值。我希望在单击文本框时清除默认值并替换为光标。HTML: <input type="text" value="user name" id="userName" /> 编辑:这里有一个将以下内容作为属性添加到输

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

<input class="login-inpt" runat="server" name="loginName" type="text" value="User Name" />
它当前持有一个默认值。我希望在单击文本框时清除默认值并替换为光标。

HTML:

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

编辑:这里有一个

将以下内容作为属性添加到输入标记中:

onfocus="if(this.value=='A new value') this.value='';"
发件人:

您可以试试这个

<input type="text" value="user name" onfocus="if(this.value=='user name') this.value='';">
对于更新的浏览器IE>9:

我得到了答案,谢谢。 输入类型=文本id=用户名onfocus=inputFocusthis onblur=inputBlurthis


函数inputFocusi{if i.value==i.defaultValue{i.value=;i.style.color=000;}}}函数inputBluri{if i.value=={i.value=i.defaultValue;i.style.color=888;}}您已经得到了很好的答案,但如果您是新手,您可能会对它在没有库的情况下如何工作感兴趣

以下是HTML:
查看此处

查看此解决方案:此问题与您的问题非常相似:为什么在ASP.NET MVC 3视图中有runat=server属性?您似乎混淆了经典的ASP.NET WebForms和ASP.NET MVC。您正在使用这两种技术中的哪一种?您的问题与asp.net MVC无关。这是一个html/javascript问题。我编辑了您的问题。我正在使用Asp.net MVC3。我通过使用OnFocus和OnBlur事件获得了解决方案。感谢大家。这确实是最好的方法,可以跨浏览器工作,因为这在旧版本的Internet Explorer中不起作用。如果您只针对最新版本的Internet Explorer,那么它是一个不错的选择。函数inputFocusi{if i.value==i.defaultValue{i.value=;i.style.color=000;}}}函数inputBluri{if i.value==i.defaultValue;i.style.color=888;}
<input type="text" value="user name" onfocus="if(this.value=='user name') this.value='';">
<input type="text" id="userName" placeholder="User Name" />
<!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>
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();
<input id="textBox" class="textBox phrase" type="text" value="Enter Your Text..">
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;
});
$('.login-inpt').focus(function() {
   $(this).val('');
});
$('.login-inpt').focusout(function() {
   $(this).val('User Name');
});