简体   繁体   中英

Firefox: lose focus when clear textbox value

I have a search box (textbox) and I want to clear it's content when I click inside. (some kind of watermark. Watermark control was not good for me in this case). I am using "onKeyDown" event to clear the content:

 function clearTbSearch(tbSearch) {

        if (tbSearch != null && tbSearch.value == '<%= TypeHereText %>') {

            tbSearch.value = "";


            tbSearch.style.color = "#000000";

        }
        return true;
    }

It works fine in IE and Chrome but in Firefox it takes 3 (!!!) clicks to get it focused. First click does not do anything. The second one clears the textbox but do not focus. The third click get the cursor focused. I tried anything... I would be glad for any suggestions... Thanks!!!

No need to use onkeydown event, just use the generic onfocus instead:

var firstTime = true;
window.onload = function() {
    var input = document.getElementById("MyInput");
    input.onfocus = function() {
        if (!firstTime)
            clearTbSearch(this);
        firstTime = false;
    };
    input.focus();
};

This way it will work either by keyboard focus or mouse focus eg when clicking the textbox.

你可以尝试这个让它看起来更干净:

<input type="text" name="myInput" value="Your initial trademark" onfocus="this.value=''" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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