简体   繁体   English

如何使用 DOM 和 JavaScript 更改背景图像

[英]How to change background Image with DOM and JavaScript


Hi Everyone, I am a beginner working on a small project in which I am facing an issue related to the background image (how I can change the background image using DOM and Javascript).大家好,我是一个从事小型项目的初学者,在该项目中我面临与背景图像相关的问题(如何使用 DOM 和 Javascript 更改背景图像)。 I Have tried a few ways but am still on the problem side.我尝试了几种方法,但仍然存在问题。 I have used JavaScript function (random_image_picker(min,max)) for getting random image from Image_gallery.我使用 JavaScript 函数 (random_image_picker(min,max)) 从 Image_gallery 中获取随机图像。 Everything going well until when I clicked on background_theme btn, the background image was supposed to be changed but not working.一切都很顺利,直到我单击 background_theme btn 时,背景图像应该被更改但无法正常工作。 Every help would be appreciated.每一个帮助将不胜感激。 Thanks HTML Tag...感谢 HTML 标签...

..Background Image.... ..背景图片....
 <script> const image_gallery = ['https://img.playbuzz.com/image/upload/ar_1.5,c_pad,f_jpg,b_auto/q_auto:good,f_auto,fl_lossy,w_480,c_limit,dpr_1/cdn/2c2dd0ea-8dec-4d49-8a95-f7b18f0b7aed/df312655-1d96-457d-88f4-cfc93c580d1d_560_420.jpg', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTwpR0X1hmpt4Kd2WviLXQGPrnUllW2UoLgtoWBoesgtFtSPFCF808bibJ8K2VhHRCki48&usqp=CAU', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRxmLTuD78jeZaVR3I_xfIuvmE4tse_JuhGjQ&usqp=CAU', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSxWrBodkwbTlxbMexeQCOneifPHaOUoTFwPA&usqp=CAU', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTtQMwmsMjdYfF0W0cyMPX673aKVK3m8sSDjg&usqp=CAU'] function random_image_picker(min, max) { let a = Math.floor(Math.random() * (max - min + 1) + min); return image_gallery[a]; } let background_theme = document.querySelector('#background_theme'); let main = document.getElementsByTagName('main'); background_theme.addEventListener('click',function(){ main.style.backgroundImage = URL(random_image_picker(0,4)) }) </script>

BackgroundImage should be a string BackgroundImage 应该是一个字符串

The style.backgroundImage property accepts a string. style.backgroundImage属性接受一个字符串。 you can apply it using a template string `url(${random_image_picker(0, 4)})` or using a regular concat like "url("+random_image_picker(0, 4)+")"您可以使用模板字符串`url(${random_image_picker(0, 4)})`或使用常规 concat 来应用它,例如"url("+random_image_picker(0, 4)+")"

getElementsByTagName('main') return an array of elements getElementsByTagName('main') 返回一个元素数组

when you use getElementsByTagName you'll receive an array.当您使用getElementsByTagName时,您将收到一个数组。 If there's only 1 element to apply the background image to.如果只有 1 个元素可以应用背景图像。 you may select the first element using an 0 index.您可以使用 0 索引选择第一个元素。 document.getElementsByTagName('main')[0];

 const image_gallery = ['https://img.playbuzz.com/image/upload/ar_1.5,c_pad,f_jpg,b_auto/q_auto:good,f_auto,fl_lossy,w_480,c_limit,dpr_1/cdn/2c2dd0ea-8dec-4d49-8a95-f7b18f0b7aed/df312655-1d96-457d-88f4-cfc93c580d1d_560_420.jpg', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTwpR0X1hmpt4Kd2WviLXQGPrnUllW2UoLgtoWBoesgtFtSPFCF808bibJ8K2VhHRCki48&usqp=CAU', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRxmLTuD78jeZaVR3I_xfIuvmE4tse_JuhGjQ&usqp=CAU', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSxWrBodkwbTlxbMexeQCOneifPHaOUoTFwPA&usqp=CAU', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTtQMwmsMjdYfF0W0cyMPX673aKVK3m8sSDjg&usqp=CAU' ] function random_image_picker(min, max) { let a = Math.floor(Math.random() * (max - min + 1) + min); return image_gallery[a]; } let background_theme = document.querySelector('#background_theme'); let main = document.getElementsByTagName('main')[0]; background_theme.addEventListener('click', function() { main.style.backgroundImage = `url(${random_image_picker(0, 4)})` })
 main { width: 400px; height: 400px; border: 2px solid lime; }
 <main> <button id="background_theme">swap theme</button> </main>

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

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