简体   繁体   English

用JS改变div背景图像

[英]Change div background image with JS

I am trying to place background images for divs with JS and change those images on mouse over and on click. 我正在尝试使用JS为div设置背景图像,并在鼠标上单击更改这些图像。 I am trying to do it like this: 我想这样做:

window.onload; {
    var replies = document.getElementsByClassName("reply-wrapper");
    var retweets = document.getElementsByClassName("retweet-wrapper");
    var favs = document.getElementsByClassName("fav-wrapper");

    for (var i = 0; i < replies.length; i++) {
        replies[i].style.backgroundImage = "url(images/reply.png);";
    }
    for (var i = 0; i < retweets.length; i++) {
        retweets[i].style.backgroundImage = "url(images/retweet.png);";
    }
    for (var i = 0; i < favs.length; i++) {
        favs[i].style.backgroundImage = "url(images/favorite.png);";
    }
}

But for some reason it won't work. 但由于某种原因它不起作用。 Aprropriate divs have right classes and adding them to collection works fine, but placing images itself doesn't work. 适当的div有正确的类,并将它们添加到收集工作正常,但放置图像本身不起作用。 What am I doing wrong? 我究竟做错了什么? PS Wanna write it in pure JS, no jQuery. PS想用纯JS编写它,没有jQuery。 PPS For some reason placing image with innerHTML works fine. PPS由于某种原因,使用innerHTML放置图像工作正常。 Why? 为什么?

It looks like you've made a slight mistake when assigning your code to the window.onload event. 在将代码分配给window.onload事件时,您似乎犯了一个小错误。

Your code needs to be in a function assigned to the onload event. 您的代码需要位于分配给onload事件的函数中。

window.onload = function () { /* Your Code */ };

instead of: 代替:

window.onload; { /* Your Code /* }

There are two issues: 有两个问题:

  1. Like Jamie Dixon mentioned it needs to be window.onload = function(){} 像Jamie Dixon提到的那样需要是window.onload = function(){}
  2. favs[i].style.backgroundImage = "url(images/favorite.png);"; should be favs[i].style.backgroundImage = "url(images/favorite.png)"; 应该是favs[i].style.backgroundImage = "url(images/favorite.png)"; The extra ; 额外的; will cause your link not to work. 将导致您的链接无法正常工作。 So remove the extra semi-colon from the rest of your links. 因此,从其余链接中删除多余的分号。

Update with explanation: 更新说明:

#room1 {
  background-image: url('image/example.png');
}

In the example above you'll notice the semi-colon. 在上面的例子中,你会注意到分号。 Like in any programming language this is to say that this is the end of the command . 就像在任何编程语言中一样,这就是说这是命令结束 Sometimes semi-colon's are optional ( like in certain cases found in js ). 有时候分号是可选的(比如js中的某些情况)。

JS isn't CSS. JS不是CSS。 Even if you apply a style to your html element, you are using JS and not CSS. 即使您将样式应用于html元素,也使用的是JS而不是CSS。 This is why you don't need the ; 这就是你不需要的原因; inside the quotes. 在报价内。

The event handler window.onload needs to be assigned a function to invoke. 需要为事件处理程序window.onload分配一个要调用的函数。 So, you just need to add the keyword 'function' before the block of code and then assign that to window.onload. 因此,您只需要在代码块之前添加关键字“function”,然后将其分配给window.onload。 Additionally, if you want to play nice with other code on the page you could grab a reference to any existing onload handler and then invoke in your onload function. 此外,如果您想与页面上的其他代码一起使用,您可以获取对任何现有onload处理程序的引用,然后在onload函数中调用。

 var oldOnLoad = window.onload; window.onload = function() { if (typeof oldOnLoad === 'function') { oldOnLoad(); } var replies = document.getElementsByClassName("reply-wrapper"); var retweets = document.getElementsByClassName("retweet-wrapper"); var favs = document.getElementsByClassName("fav-wrapper"); for (var i = 0; i < replies.length; i++) { replies[i].style.backgroundImage = "url(images/reply.png)"; } for (var i = 0; i < retweets.length; i++) { retweets[i].style.backgroundImage = "url(images/retweet.png)"; } for (var i = 0; i < favs.length; i++) { favs[i].style.backgroundImage = "url(images/favorite.png)"; } }; 

Note: it might be worth mentioning that loading the background images in the onload event handler could cause your page to appear to load slow because it will wait for all other content on the page to finish loading. 注意:值得一提的是,在onload事件处理程序中加载背景图像可能会导致页面加载速度变慢,因为它会等待页面上的所有其他内容完成加载。 You might want to do something like instead: 你可能想要做一些事情:

 <div id="reply-wrapper" style="display: block; width: 250px; height: 250px;"></div> <script> (function() { document.getElementById('reply-wrapper').style.backgroundImage = "url(//dummyimage.com/250x250/000/fff)"; })(); </script> 

I don't see a function name for the code. 我没有看到代码的函数名称。 Is it incomplete? 它不完整吗?
this might be the error. 这可能是错误。

window.onLoad;  

Try this: 尝试这个:

function fn_load(){
 // Your statements
 }

and in the body do this: 并在身体中这样做:

i should be: 我应该:

window.onload = function() {
var replies = document.getElementsByClassName("reply-wrapper");
var retweets = document.getElementsByClassName("retweet-wrapper");
var favs = document.getElementsByClassName("fav-wrapper");

for (var i = 0; i < replies.length; i++) {
    replies[i].style.backgroundImage = "url(images/reply.png);";
}
for (var i = 0; i < retweets.length; i++) {
    retweets[i].style.backgroundImage = "url(images/retweet.png);";
}
for (var i = 0; i < favs.length; i++) {
    favs[i].style.backgroundImage = "url(images/favorite.png);";
}
} 

you DOM is calling even before window got load. 即使在窗口加载之前,DOM也在调用。

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

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