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? ? ? ... " and "script" are used to store the used .css files and .js files
index.html contains
<div id="pic">
<p>开心吗</p>
</div>
Copy after login
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); }
Copy after login
-
-
In the js file, the path must be written as images/bg1.png
-
In short,
-
(1) For js scripts, html loads the script into the page and parses it together (it is the same as writing your js script directly 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 looks for the required image file in the css file according to the link.
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.