JavaScript 背景图像被 $(document).ready 破坏
我的代码:
<html>
<head>
<script type="text/javascript" charset="utf-8">
function backgroundImage() {
document.body.style.backgroundImage='url("http://www.image.jpg")';
}
</script>
</head>
<body onLoad="backgroundImage()">
Content here
</body>
</html>
但是添加 document.ready:
$(document).ready(function () {
document.body.style.backgroundImage='url("http://www.image.jpg")';
});
会破坏代码
为什么这不起作用,我缺少什么,或者不需要 document.ready 的前缀?难道“文档”不应该被调用两次吗?
提前致谢!
亲切的问候, 戴尔
My code:
<html>
<head>
<script type="text/javascript" charset="utf-8">
function backgroundImage() {
document.body.style.backgroundImage='url("http://www.image.jpg")';
}
</script>
</head>
<body onLoad="backgroundImage()">
Content here
</body>
</html>
But the addition of document.ready:
$(document).ready(function () {
document.body.style.backgroundImage='url("http://www.image.jpg")';
});
breaks the code
Why does this not work, something I'm missing, or prefix of document.ready just not needed? Could it be that 'document' should not be called twice?
Thanks in advance!
Kind Regards,
Dale
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是没问题的,可以运行
http://jsfiddle.net/HRhQW/
This is no problem, can run
http://jsfiddle.net/HRhQW/
您应该将函数名称从“backgroundImage”更改为“setBackgrounImage”之类的名称。该名称在某种程度上与属性相冲突。
更新:我想我错了,请参阅jsfiddle.net/ARsmn。正文 onload 函数在 $(document).ready() 之后调用。因此,在使用 body body onload 和 $(document).ready() 时,您可能需要牢记这一点。
You should change the name of the function from 'backgroundImage' to something like 'setBackgrounImage'. The name is somehow conflicting with the property.
Update: I guess I was wrong, See this jsfiddle.net/ARsmn. The body onload function is called after the $(document).ready(). So you might want to keep this in your mind when using body body onload and $(document).ready().