简体   繁体   English

更改背景图片网址

[英]Change background image url

I am trying to change the background of the input button when clicked by using jquery but for the life of me, can't figure out the relative path to make it work. 我正在尝试使用jquery更改输入按钮的背景,但为了我自己的生命,无法弄清楚使其工作的相对路径。

js file is in root/js folder css file is in root/css folder js文件位于root / js文件夹中css文件位于root / css文件夹中

My code looks like this: 我的代码如下所示:

jQuery($button).css('background',"url(../images/updating_button.gif)");

But this just doesn't work...it can't find the image with that path. 但这根本行不通...无法找到具有该路径的图像。 if I use absolute path it works obviously but i really need it to be relative. 如果我使用绝对路径,则显然可以,但是我确实需要相对路径。

I have tried all combination that I know like: 我尝试了所有我知道的组合:
/images/updating_button.gif /images/updating_button.gif
..images/updating_button.gif ..images / updating_button.gif
images/updating_button.gif 图片/ updating_button.gif
updating_button.gif updating_button.gif

If the URL is static, you could declare a class with the background and maintain your relative path. 如果URL是静态的,则可以在后台声明一个类并维护您的相对路径。

In your CSS: 在您的CSS中:

.updating { background: url(../images/updating_button.gif); }

In your JS: 在您的JS中:

jQuery($button).addClass('updating');

You are aware that paths in any inline styles (whether set with JS or not) will be relative to the current HTML document (the URL on the browser's URL bar), not any other file, right? 您知道任何内联样式的路径(无论是否使用JS设置)都将相对于当前HTML文档(浏览器URL栏上的URL),而不是其他文件,对吗?

And why are you avoiding absolute (or domain-relative) URLs? 以及为什么要避免使用绝对(或相对于域)URL?

以下代码可能对您有用:

 jquery("button").css("background-image","url(menubar/ajax-loader.gif)");

I wish I could test this for you, but I've had some issues with the url property in the past; 我希望可以为您测试一下,但是过去url属性有一些问题; adding single quotes inside the url() has proved to be the most reliable with relative paths. 在相对路径中,在url()添加单引号被证明是最可靠的。

jQuery($button).css('background',"url('../images/updating_button.gif')");

Also, what does your file structure look like? 另外,您的文件结构是什么样的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM