[英]How to change background image in external stylesheet with javascript?
我想从外部样式表更改背景图像。
例如,在外部样式表背景图像中:
#nav
{
background-image:url(../images/nav.png);
}
现在我想在运行时通过javascript更改它,如:
#nav
{
background-image:url(../images/newImg.png);
}
我可以使用此javascript代码成功更改此图像:
eval('theRule.style.backgroundImage="../images/newImg.png"');
但问题是:结果看起来像这样:
#nav
{
background-image:url(http//localhost/project/images/newImg.png);
}
我不需要http // localhost / project / etc ...
我只需要../images/newImg.png
我努力解决这个问题,但我还没有找到任何解决方案。
那么请指导我如何解决这个问题?
感谢您阅读我的问题
问候
尝试。
rule.style.setProperty("background-image", "../images/newImg.png", "");
rule.style.cssText = rule.style.cssText.replace("../images/nav.png", "../images/newImg.png"); //Replace all images
但如果没有帮助,那就找不到简单的方法了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.