Javascript 如何使用jquery自动隐藏输入字段中的占位符文本

Javascript 如何使用jquery自动隐藏输入字段中的占位符文本,javascript,jquery,html,Javascript,Jquery,Html,我这里有这段代码,它的作用是隐藏焦点上的占位符字段,并使用jquery在模糊中显示它们,但我需要有人逐行向我解释这段代码,因为我是初学者 $(函数(){ "严格使用",; //隐藏窗体焦点上的占位符 $('[placeholder]')。焦点(函数(){ $(this.attr('data-text',$(this.attr('placeholder')); $(this.attr('占位符',''); }).blur(函数(){ $(this.attr('placeholder',$(thi

我这里有这段代码,它的作用是隐藏焦点上的占位符字段,并使用jquery在模糊中显示它们,但我需要有人逐行向我解释这段代码,因为我是初学者

$(函数(){
"严格使用",;
//隐藏窗体焦点上的占位符
$('[placeholder]')。焦点(函数(){
$(this.attr('data-text',$(this.attr('placeholder'));
$(this.attr('占位符','');
}).blur(函数(){
$(this.attr('placeholder',$(this.attr('data-text'));
});
});

管理员登录

因此,它基本上是在聚焦元素时,将占位符属性的值存储在数据文本属性中,并将占位符设置为空。然后在“模糊”上,它只是将占位符设置为属性中的值。它存储在“数据文本”属性中,以便以后检索。它除了像一个变量一样存储一个值之外,没有什么特殊用途

如果你想知道什么 $('[placeholder]')。焦点(函数(){ 基本上,这意味着如果聚焦任何具有占位符属性的对象,则调用该函数。

$(“[placeholder]”)

jQuery使用属性占位符找到的任何对象

.focus(…)

当元素被聚焦时,执行内部代码

$(this.attr('data-text',$(this.attr('placeholder'));

此行临时存储占位符(以便以后可以重新设置)

$(this.attr('占位符','');

这将从元素中清除占位符

.blur(…)

当元素模糊时,执行内部代码

$(this.attr('placeholder',$(this.attr('data-text'));

此行将占位符设置为临时位置中存储的值(如上所述)

希望这有帮助,如果符合您的需要,请将其标记为答案。

$(function() {}).... 
    is an anonymous function which gets called when js file gets loaded in the browser.

$('[placeholder]')
    Provides collections of elements objects having placeholder inside the form.

$('[placeholder]').focus(callback)
    This statement will bind the focus event to all the elements which supports placeholder.

callback()
    This is a function which gets called when above event gets fired.

Inside call back in above code two below statements are written
    $(this).attr('data-text', $(this).attr('placeholder'));
    // Above satement will pick text given in placeholder property and assign this to 'data-text' property

    $(this).attr('placeholder', '');
        //Above stement will make placeholder text empty by assigning emty string.

.blur(callback_b)
    This statement will bind the blur event to all the elements which supports placeholder.

callback_b()
    This is a function which gets called when blur event gets fired.

Inside callback_b() below code is written
    $(this).attr('placeholder', $(this).attr('data-text'));
    //This statement will take the value assigned to 'data-text' in focus event and assign it back to placeholder property.