简体   繁体   English

JavaScript backgroundImage被$(document).ready损坏

[英]JavaScript backgroundImage broken by $(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:

$(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? 为什么这行不通,缺少某些内容或没有document.ready前缀? Could it be that 'document' should not be called twice? 难道“文档”不应被调用两次?

Thanks in advance! 提前致谢!

Kind Regards, Dale 亲切的问候,戴尔

This is no problem, can run 没问题,可以运行

http://jsfiddle.net/HRhQW/ http://jsfiddle.net/HRhQW/

You should change the name of the function from 'backgroundImage' to something like 'setBackgrounImage'. 您应该将函数的名称从“ backgroundImage”更改为“ setBackgrounImage”。 The name is somehow conflicting with the property. 该名称与该属性有些冲突。

Update : I guess I was wrong, See this jsfiddle.net/ARsmn. 更新 :我想我错了,请参阅此jsfiddle.net/ARsmn。 The body onload function is called after the $(document).ready(). 主体onload函数在$(document).ready()之后调用。 So you might want to keep this in your mind when using body body onload and $(document).ready(). 因此,在使用body body onload和$(document).ready()时,您可能需要记住这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM