Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.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 如何在onclick JS函数之后返回上一个文本?_Javascript_Function - Fatal编程技术网

Javascript 如何在onclick JS函数之后返回上一个文本?

Javascript 如何在onclick JS函数之后返回上一个文本?,javascript,function,Javascript,Function,我的代码是: function changeText() { document.getElementById('button').innerHTML = 'Downloading...'; } 按钮: <button id = "button" onclick='changeText()' value='Change Text'>Download file as CSV</button> 以CSV格式下载文件 我想把按钮改为“下载…”,几秒钟后返回“下载为

我的代码是:

    function changeText()
{
 document.getElementById('button').innerHTML = 'Downloading...';
}
按钮:

<button id = "button" onclick='changeText()' value='Change Text'>Download file as CSV</button>
以CSV格式下载文件
我想把按钮改为“下载…”,几秒钟后返回“下载为CSV”,这在JS中可能吗

document.getElementById("your-button").value="Downloading...";
setTimeout(function () { 
    document.getElementById("your-button").value="Download as CSV"; 
}, 3000);

上述代码设置按钮文本,然后等待3秒钟,然后再将按钮文本设置为另一个字符串。

您可以使用
setTimeout
或在异步调用成功时:

函数changeText(){
document.getElementById('button')。innerHTML='Downloading…';
设置超时(上一个文本,1000);
}
函数previousText(){
document.getElementById('button').innerHTML='Download file as CSV';
}

将文件下载为CSV
此代码将在2000ms后恢复旧文本,但您可以自定义超时

function changeText() {
    var button = document.getElementById('button');

    var oldText = button.innerHTML;
    button.innerHTML = 'Downloading...';
    setTimeout(function(){
        button.innerHTML = oldText;
    }, 2000);
}

@肖恩9113很高兴我帮了你:)