Javascript/jQuery纯Javascript等效代码

Javascript/jQuery pure javascript equivalent of code

本文关键字:Javascript 代码 jQuery      更新时间:2023-09-26

我想把这段代码翻译成纯javascript,这样我就不必使用jquery:

$('#msg').show(0).delay(5000).hide(0);

javascript的等价物是什么?

您可以使用以下代码:

document.getElementById("msg").style.display = 'block';
setTimeout(function () {
  document.getElementById("msg").style.display = 'none';
}, 5000);
#msg {background: #f90; width: 50px; height: 50px;}
<div id="msg">
  Hello
</div>

我已经给出了CSS的演示目的是为了清楚。

使用不透明度的Praveen代码的另一个版本。

var p = document.getElementById('msg');
p.style.opacity = 1;
setTimeout(function() {
 p.style.opacity = 0;
}, 5000)