Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 页面刷新时重置变量值_Javascript_Jquery_Html - Fatal编程技术网

Javascript 页面刷新时重置变量值

Javascript 页面刷新时重置变量值,javascript,jquery,html,Javascript,Jquery,Html,这是用于清除和恢复输入值onFocus Blur的简单js脚本。。 它工作得很好,但如果我在那里键入一个文本,例如:aaa,则会刷新页面并清除输入值 通过退格按钮或其他方式,它将“aaa”保存在我的defaultValue变量中,然后恢复“aaa”而不是“搜索”中的cleard输入值 javaScript(jQuery)中是否有函数或方法在刷新页面后清除变量内存, 不通过url重新加载。按Enter键。。仅通过F5刷新或在浏览器中使用刷新箭头按钮刷新 你可以在FireFox中检查,因为在Chro

这是用于清除和恢复输入值onFocus Blur的简单js脚本。。 它工作得很好,但如果我在那里键入一个文本,例如:aaa,则会刷新页面并清除输入值 通过退格按钮或其他方式,它将“aaa”保存在我的defaultValue变量中,然后恢复“aaa”而不是“搜索”中的cleard输入值

javaScript(jQuery)中是否有函数或方法在刷新页面后清除变量内存, 不通过url重新加载。按Enter键。。仅通过F5刷新或在浏览器中使用刷新箭头按钮刷新

你可以在FireFox中检查,因为在Chrome中没有问题,Chrome自己做

希望有人能帮忙(

谢谢


在支持html5元素的现代浏览器中,您可以使用
输入上的属性来执行此操作,而无需任何javascript:

HTML

<input name="newsSearch" type="text" placeholder="Search" />

您可以添加一些javascript来支持不支持占位符属性的旧浏览器:

Javascript

$(document).ready(function () {
    function add() {
        if ($(this).val() === '') {
            $(this).val($(this).attr('placeholder')).addClass('placeholder');
        }
    }

    function remove() {
        if ($(this).val() === $(this).attr('placeholder')) {
            $(this).val('').removeClass('placeholder');
        }
    }

    // Create a dummy element for feature detection
    if (!('placeholder' in $('<input>')[0])) {

        // Select the elements that have a placeholder attribute
        $('input[placeholder], textarea[placeholder]').blur(add).focus(remove).each(add);

        // Remove the placeholder text before the form is submitted
        $('form').submit(function () {
            $(this).find('input[placeholder], textarea[placeholder]').each(remove);
        });
    }
});
$(文档).ready(函数(){
函数add(){
if($(this.val()=''){
$(this.val($(this.attr('placeholder')).addClass('placeholder');
}
}
函数删除(){
if($(this.val()==$(this.attr('placeholder')){
$(this.val(“”).removeClass('placeholder');
}
}
//为特征检测创建虚拟元素
如果(!('placeholder'单位为$('')[0])){
//选择具有占位符属性的元素
$('input[placeholder],textarea[placeholder]')。模糊(添加)。聚焦(删除)。每个(添加);
//在提交表单之前删除占位符文本
$('form')。提交(函数(){
$(this.find('input[placeholder],textarea[placeholder])。每个(remove);
});
}
});

进一步阅读


是的,我知道这一点,但我不想使用HTML5,我更喜欢旧的浏览器支持。看看我链接到的javascript回退。它为不支持
占位符的浏览器提供了回退。
$(document).ready(function () {
    function add() {
        if ($(this).val() === '') {
            $(this).val($(this).attr('placeholder')).addClass('placeholder');
        }
    }

    function remove() {
        if ($(this).val() === $(this).attr('placeholder')) {
            $(this).val('').removeClass('placeholder');
        }
    }

    // Create a dummy element for feature detection
    if (!('placeholder' in $('<input>')[0])) {

        // Select the elements that have a placeholder attribute
        $('input[placeholder], textarea[placeholder]').blur(add).focus(remove).each(add);

        // Remove the placeholder text before the form is submitted
        $('form').submit(function () {
            $(this).find('input[placeholder], textarea[placeholder]').each(remove);
        });
    }
});