Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript-如何使用相对于url路径的图像文件?_Javascript_Css - Fatal编程技术网

javascript-如何使用相对于url路径的图像文件?

javascript-如何使用相对于url路径的图像文件?,javascript,css,Javascript,Css,有没有一种方法可以在javascript文件中使用图像url相对路径(就像css文件一样) 为了进行测试,我使用了2个div,在第一个div中使用css,在第二个div中使用js在后台显示gif: -我的文件目录是: root/index.html root/module1/test.css root/module1/test.js root/module1/img.gif -index.html代码: <html> <head> <link href=

有没有一种方法可以在javascript文件中使用图像url相对路径(就像css文件一样)

为了进行测试,我使用了2个div,在第一个div中使用css,在第二个div中使用js在后台显示gif:

-我的文件目录是: root/index.html

root/module1/test.css

root/module1/test.js

root/module1/img.gif

-index.html代码:

<html>
  <head>
    <link href="module1/test.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <P id="p1"> Line 1 </P>
    <P id="p2"> Line 2 </P>
    <script type="text/javascript" src="module1/test.js"></script>
  </body>
</html>
在css中,我可以使用相对路径

-test.js代码:

document.getElementById("p1").style.backgroundImage = 'url(./module1/img.gif)';
但是在js中,我必须使用绝对路径,否则它不起作用

-img.gif-您可以使用任何gif图像

我试图搜索网络,但我只是感到困惑:(

plz帮助:)


Ps:如果您知道jquery中的解决方案,我也很感激。

在CSS样式表中,路径是相对于样式表解释的

如果稍后使用JavaScript指定路径,则将相对于文档进行解释

您仍然可以使用相对路径,但如上所述,它们必须是相对于文档的。所以它必须是

url("module1/img.gif");
但你已经知道了

我不知道如何在样式表之外构建相对于样式表的路径

我想到的唯一解决方法是在样式表中定义一个类,而不是使用javaScript指定背景图像,而是更改元素的类

在样式表中:

.p2_img_gif {background: url('img.gif')}
当段落获得背景图像时,做一个

document.getElementById("p2").className = "p2_img_gif";

如果您需要切换类,或者指定多个类,请考虑使用jQuery的代码> AdvCub()/<代码>和<代码> ReaveCeCase](< /代码> .< /P> < P>这对Firefox 3.6来说是适用的。

<!doctype html>
<style>
div { background: red; width: 80px; height: 80px }
</style>
<script>
window.onload = function() {
  document.getElementById("test").style.backgroundImage = 'url("green.png")';
}
</script>
<p>There should be no red.</p>
<div id="test"></div>

div{背景:红色;宽度:80px;高度:80px}
window.onload=函数(){
document.getElementById(“test”).style.backgroundImage='url(“green.png”);
}
应该没有红色


其中green.png是绿色像素。您使用哪种浏览器?

作为变量使用元素内联样式

var element = document.getElementById("p1");
var imageUrl = './module1/img.gif';
element.setAttribute('style', 'background-image: url(' + imageUrl +');');

你的代码运行正常,但它不能反映我的情况。当javascript代码(/file)和html文件位于不同的目录中时,就会出现问题。我想在js文件中使用“url(img.gif)”,但它不起作用(就像在css文件中一样)。我正在使用Firefox 3.6.8、IE8.0.6001.18702和chrome 5.0.375.127尝试使用Firebug的网络面板查看浏览器试图加载的内容。我打赌它正在尝试加载一些东西,但是解释URL相对于你认为它会加载的东西(如Pekka所说)的相对位置。使用上面的代码,它不会在Firebug的网络面板->所有选项卡中显示任何东西。无论如何,我已经采纳了Pekka建议的概念,它在我的网站上使用jquery非常有效。好主意。我不记得那个了。我仍然更愿意找到一个解决方案,如果我可以使用javascript,而不是在css中声明它,但是如果我没有找到它,这也是一个不错的解决方法。坦克:)
var element = document.getElementById("p1");
var imageUrl = './module1/img.gif';
element.setAttribute('style', 'background-image: url(' + imageUrl +');');