Home  >  Article  >  Web Front-end  >  How to import javascript background image

How to import javascript background image

藏色散人
藏色散人Original
2021-06-18 11:55:356304browse

How to import javascript background images: first create a new html file and use div to create a module; then set the style of the div through the id in the css tag; then bind the onclick click event to the button button; finally, in Just implement the import of background images in the setBg() function.

How to import javascript background image

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to import javascript background images?

Create a new html file, named test.html, to explain how to import background images in js.

How to import javascript background image

In the test.html file, use div to create a module for importing background images.

How to import javascript background image

In the css tag, set the style of the div through the id, define its width and height as 300px, and the border as 1px.

How to import javascript background image

In the test.html file, use the button tag to create a button, bind the onclick click event to the button button, and when the button is clicked, execute the setBg() function.

How to import javascript background image

In the js tag, create the setBg() function. Within the function, obtain the div object through the id, and then assign a value to the backgroundImage attribute of the style attribute to realize the background image. Import. For example, the path to the 2.jpg image is assigned here.

How to import javascript background image

Open the test.html file in the browser, click the button to view the results.

How to import javascript background image

How to import javascript background image

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to import javascript background image. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn