简体   繁体   English

Javascript:以编程方式更改onfocus函数

[英]Javascript: programmatically change onfocus function

I have several text boxes that can auto-fill with data and I am using a javascript function to clear the text box one time, then revert to a javascript function that only clears when certain text is input. 我有几个可以自动填充数据的文本框,并且我正在使用javascript函数一次清除文本框,然后恢复为仅在输入某些文本时清除的javascript函数。

For instance: A text box with standard input as "ADDRESS" will be auto-filled with "ABC123" then onfocus be cleared. 例如:一个标准输入为“ ADDRESS”的文本框将自动填充为“ ABC123”,然后清除onfocus。 If the text box remains empty, then onblur, it will return to "ADDRESS" 如果文本框保持为空,则onblur,它将返回到“ ADDRESS”

This is similar to the question at Change an element's onfocus handler with Javascript? 这类似于使用JavaScript更改元素的onfocus处理程序? but I couldn't get it to work. 但我无法正常工作。 Any suggestions? 有什么建议么?

My text boxes are just ASP.NET text boxes and the onfocus/onblur events are set in the code behind: 我的文本框只是ASP.NET文本框,并且onfocus / onblur事件在后面的代码中设置:

<asp:TextBox ID="txtAddress" Text="ADDRESS" runat="server" CssClass="txtboxwrong" />

Code Behind: 背后的代码:

txtAddress.Attributes.Add("onFocus", "clearOnce(this,'ADDRESS');")
txtAddress.Attributes.Add("onBlur", "restoreText(this,'ADDRESS');")

My javascript is as follows: 我的JavaScript如下:

function clearText(obj, deftext, defclass, defvalue) {
    if (obj.value == deftext) {
        if (!defvalue) { defvalue = '' }
        obj.value = defvalue;
        if (!defclass) { defclass = 'txtbox' }
        obj.className = defclass;
    }
};

function restoreText(obj, deftext, defclass, defvalue) {
    if (!defvalue) { defvalue = '' }
    if (obj.value == defvalue || obj.value == '')  {
        obj.value = deftext;
        if (!defclass) { defclass = 'txtboxwrong' }
        obj.className = defclass;
    }
};

function clearOnce(obj, deftext) {
    obj.value = '';
    obj.className = 'txtbox';
    obj.onfocus = function () { clearText(obj, deftext); };
};

EDIT: 编辑:

Thanks to @rescuecreative, I have fixed the probem. 感谢@rescuecreative,我修复了问题。 By returning the onfocus change in clearOnce, it sets the element's onfocus to the right function and works properly! 通过在clearOnce中返回onfocus更改,它将元素的onfocus设置为正确的函数并正常工作! Edit below: 编辑如下:

function clearOnce(obj, deftext) {
obj.value = '';
obj.className = 'txtbox';
return function () { clearText(obj, deftext); };

}; };

Can your asp textbox use the placeholder attribute? 您的ASP文本框可以使用占位符属性吗? In html5, the placeholder attribute automatically creates the exact functionality you're looking for. 在html5中,占位符属性会自动创建您要查找的确切功能。

<input type="text" placeholder="ADDRESS" />

The above text field will show the word "ADDRESS" until focused at which point it will empty out and allow the user to type. 上面的文本字段将显示单词“ ADDRESS”,直到重点突出为止,它将清空并允许用户输入。 If the user leaves the field and it remains empty, the placeholder reappears. 如果用户离开该字段并且该字段保持空白,则占位符会重新出现。 If you can't depend on html5, there is a JavaScript plugin that will create that functionality in browsers that don't support it natively. 如果您不能依赖html5,则可以使用一个JavaScript插件在不支持它的浏览器中创建该功能。

It seems like you wanted to do something similler to watermarks. 好像您想对水印做些模仿。 You can achiever it in much simpler way. 您可以以更简单的方式实现它。 Try this. 尝试这个。

function clearOnce(obj, deftext) {
    if(obj.value == deftext) {
        obj.value = '';
        obj.className = 'txtbox';
    }
}

function restoreText(obj, deftext) {
    if(obj.value == '') {
        obj.value = deftext;
        obj.className = 'txtboxwrong';
    }
}

Ideally you would just use the placeholder attribute, but that may not be supported on older browsers. 理想情况下,您将只使用placeholder属性,但是较旧的浏览器可能不支持该属性。 If you can use jQuery, something like this would work for you: 如果您可以使用jQuery,那么类似的方法将为您工作:

$('[placeholder]').focus(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
        input.val('');
        input.removeClass('placeholder');
    }
}).blur(function() {
    var input = $(this);
    if (input.val() == '' || input.val() == input.attr('placeholder')) {
        input.addClass('placeholder');
        input.val(input.attr('placeholder'));
    }
}).blur();

If nothing is ever entered into the field, the code below will ensure the placeholder text doesn't get submitted with the form: 如果未在该字段中输入任何内容,则下面的代码将确保不使用以下表单提交占位符文本:

$('[placeholder]').parents('form').submit(function() {
    $(this).find('[placeholder]').each(function() {
        var input = $(this);
        if (input.val() == input.attr('placeholder')) {
            input.val('');
        }
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM