When I was typing code today, I wanted to write a function to switch avatars, but here comes the problem, the avatar just doesn’t appear. Suddenly, I had an idea. Is it possible that the way to write image paths in CSS and JavaScript is different? ? ?
Hmm...it's like this:
Create a new "images" file in the root directory, and put two pictures bg1.png and bg2.png in it
Create two new folders in the root directory "style " and "script" are used to store the used .css files and .js files
index.html contains
<div id="pic"> <p>开心吗</p> </div>
If I want to set the image background of the tag with the id of pic, write this in the css file:
#pic{ background:url(../images/bg1.png); }
In the js file, the path should be written as images/bg1.png
In short,
(1) For js scripts, the html script should be loaded into the page and parsed together (just directly with your js script It’s the same as writing on this page)
(2) For css files, it only provides a link and does not load it into the html page. For example, in this example, html searches for the css file according to the link. Required image files.
The image file path of css is relative to itself, and the image file path of js is relative to the file where it is located.