Home > php教程 > PHP开发 > body text

The difference between image paths in css and javascript

高洛峰
Release: 2016-12-01 15:59:06
Original
1516 people have browsed it

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>
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 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.


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!