Home  >  Article  >  php教程  >  The difference between image paths in css and javascript

The difference between image paths in css and javascript

高洛峰
高洛峰Original
2016-12-01 15:59:061510browse

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.


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