简体   繁体   English

jQuery模糊文本并在Focus上清空

[英]Jquery Blur text and empty on Focus

I have multiple text box like this. 我有多个这样的文本框。 I want to apply the text on blur and empty when it get focused. 我想将文本应用于模糊并在聚焦时为空。 I can able to achieve this for single text box. 我可以为单个文本框实现此目的。 How i can pass the current element id instead of the hard coding the "#name" value in JavaScript function ? 我如何传递当前元素ID而不是对JavaScript函数中的“ #name”值进行硬编码?

$(document).ready(function(){


    $('#Name').focus(function()
    {
         var $ele = $(this).attr('id');
            $(this).val('');
            $(this).css('color', '#000000');
    });

    $('#Name').blur(function()
    {
           var $ele = $(this).attr('id');
            $(this).val($ele);
            $(this).css('color', '#a9a9a9')
    });
});



<input id="Name" type="text" value="" name="Name">
<input id="Phone" type="text" value="" name="Phone" >
<input id="Email" type="text" value="" name="Email">

You can select all input by $('input') and you can put filter text inputs using [type="text"] ; 您可以通过$('input')选择所有输入,并可以使用[type="text"]放置过滤器文本输入; You can take benefits of jQuery chaining. 您可以利用jQuery链的优势。

$(document).ready(function(){   

    $('input[type="text"]').focus(function()
    {
         var $ele = $(this).attr('id');
            $(this).val('');
            $(this).css('color', '#000000');
    }).blur(function()
    {
           var $ele = $(this).attr('id');
            $(this).val($ele);
            $(this).css('color', '#a9a9a9')
    });
});

You can directly set value of input fields in html 您可以直接在html中设置输入字段的值

<input id="Name" type="text" value="Name" name="Name">

OR Apply default values on page load as: 或在页面加载时将默认值应用为:

$('input[type="text"]').each(function(){
   var $ele = $(this).attr('id');
                $(this).val($ele);
                $(this).css('color', '#a9a9a9')

});

HTML 的HTML

<input id="Name" type="text" value="" name="Name">
<input id="Phone" type="text" value="" name="Phone" >
<input id="Email" type="text" value="" name="Email">

You need to modify you js 您需要修改您的js

$(function(){
    $('input').focus(function()
    {
        $(this)
            .val($(this).attr('name'))
            .css('color', '#000000')

    }).blur(function()
    {
            $(this).css('color', '#a9a9a9')
    });
})();​

if you set value on 'blur' you will lost current value... I am not sure that it is exactly what you want 如果您将值设置为“模糊”,则会丢失当前值...我不确定这正是您想要的

apply it to input instead of #name . 将其应用于input而不是#name

this will work for all inputs. 这将适用于所有输入。

or give a .class to the elements you want to change. 或为要更改的元素提供.class

您可以选择所有带有$('input[type="text"]') ,但我认为您只想使用placeholder HTML属性

Look into jQuery Selectors . 查看jQuery选择器 What you're doing now is using the ID as the selector. 您现在正在使用ID作为选择器。 You have tons of different options... 您有很多不同的选择...

You can replace "#Name" with "input" if you want it to apply to all inputs on the page, or you could even add a common class 'class="test"' to all the elements you want to trigger the script and then replace the "#Name" with ".test" or "input.test" for only input elements with that class name. 如果希望将“ #Name”替换为页面上的所有输入,则可以将其替换为“ #Name”,或者甚至可以向要触发脚本的所有元素添加通用类“ class =“ test””,然后仅将具有该类名称的输入元素替换为“ .test”或“ input.test”。

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

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