JScript超时触发器<img/>

时间:2013-02-12 14:12:03

标签: javascript html timeout

<html>
<body>
<script>
setTimeout(function(){
document.body.style.background = <img src="1.jpg"</img>
},33);
</script>
</body>
</html>

我想知道如何在setTimeout之后显示图像。感谢

3 个答案:

答案 0 :(得分:2)

试试这个:

setTimeout(function () {
  document.body.style.backgroundImage = "url('1.jpg')";
}, 33);

答案 1 :(得分:0)

这样的事情可以做到:

setTimeout(function(){
    document.body.style.background="url(1.jpg)";
},33);

<强> Demo

请注意,超时以毫秒为单位。对于用户而言,与通过CSS直接添加背景相比,33毫秒可能不会产生显着差异。

答案 2 :(得分:0)

要设置背景图像,您需要使用CSS background-image属性。鉴于您没有提供CSS样式表,并且您希望通过javascript动态更改属性,您需要使用style属性。

此W3C wiki Dynamic style - manipulating CSS with JavaScriptstyle属性有一个很好的解释。它有许多与CSS属性相对应的属性。考虑到style属性的CSS属性名称在CamelCase中定义,因此background-image实际上是backgroundImage

因此,为了将背景图片动态设置为document.body元素,您可以使用document.body.style.backgroundImage="url('1.jpg')";

(你的计时器功能已经好了)