jQuery 如何从文本框中删除焦点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17661130/
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-08-26 19:42:26  来源:igfitidea点击:

How to remove focus from textbox?

javascriptjquerycss

提问by user2444474

When opening form the cursor is waiting in the text box along with help text.So there are some validation error happening. So i don't want focus when opening form, user have to click the text and type the text. But here didn't any onfocus eventhough cursor in the textbox .

打开表单时,光标与帮助文本一起在文本框中等待。因此发生了一些验证错误。所以我不想在打开表单时获得焦点,用户必须单击文本并输入文本。但是这里没有任何 onfocus 即使光标在文本框中。

if(!$.trim($("#productsTextArea1").val())){
        helpTip = "help string ....";
        $("#productsTextArea1").val(helpTip);
        $("#productsTextArea1").css("color","#999");
        $("#productsTextArea1").click(function(event){
            if($("#productsTextArea1").val().indexOf("Enter a return")>-1){
                $("#productsTextArea1").css("color","black").val("");
            }
        });
    } else {
         $("#productsTextArea1").blur();
    }

Please advise...

请指教...

JSFIDDLE

JSFIDDLE

回答by defau1t

UPDATE:

更新:

Use HTML5 Placeholder attribute, you don't need to use any js.

使用 HTML5 Placeholder 属性,你不需要使用任何 js。

    <textarea id="productsTextArea1" name="product" rows="5" 
placeholder="Some Hint Text is Placed Here"></textarea>

To disable autofocus on all input elements use this.

要禁用所有输入元素的自动对焦,请使用它。

  $(function(){
        $('input').blur();
    });

To disbale autofocus on a particular element, pass the element id as below:

要取消对特定元素的自动对焦,请传递元素 ID,如下所示:

$(function(){
    $('input#someid').blur();
});