如何将 CSS url 设置为绝对位置?

发布于 2024-10-02 07:53:45 字数 773 浏览 0 评论 0原文

我正在开发一个 HTML5 移动应用程序,最初通过 CSS 设置 DIV 项目的背景,如下所示:

background-image: url('images/ClanSpider.png');

在我的应用程序中,我有一种方法可以根据下拉列表中的选择来更改背景 DIV以前使用 jQuery 的方法:

function ResetMyHonor()
{       
   ClanImage = 'images/Clan' + MyClanName + '.png';
   $("#MyClanName").html(MyClanName); 
   $("#MyHonorBox").css('backgroundImage', 'url(' + ClanImage + ')');
}

当我位于页面的根目录时,所有这些都可以正常工作。但是,我在应用程序中有一些使用井号标签来导航页面的链接(例如#MyHonor)。当我导航到这些标签之一并调用上面的重置函数时,图像会损坏。当我拉出 Chrome 检查器查看 DIV 标签时,它显示它尝试加载的图像是“images/MyHonor/ClanSpider.png”,但该图像不存在。

我知道 CSS url 将生成引用其在应用程序中的位置的链接,但我将 CSS 文件移动到应用程序中的位置并不重要。

有没有办法让我重写 url 处理的结果,或者指定 DIV 背景图像的替代方法,而不进行任何类型的服务器端处理?理想情况下,这个应用程序将通过 HTML5 的清单缓存功能运行,因此我将无法访问任何基于服务器的语言。

I'm working on an HTML5 mobile app and I initially have the background of a DIV item set through the CSS as follows:

background-image: url('images/ClanSpider.png');

In my app, I have a method that changes the background DIV based on a selection made in a dropdown list from a previous method using jQuery:

function ResetMyHonor()
{       
   ClanImage = 'images/Clan' + MyClanName + '.png';
   $("#MyClanName").html(MyClanName); 
   $("#MyHonorBox").css('backgroundImage', 'url(' + ClanImage + ')');
}

All of this works fine when I'm on the root of my page. However, I have some links within the app using hash tags to navigate the page (such as #MyHonor). When I've navigated to one of these tags and call my reset function above, the image breaks. When I pull up the Chrome Inspector to look at the DIV tag, it says that the image it is trying to load is "images/MyHonor/ClanSpider.png" which doesn't exist.

I know the CSS url will generate links in reference to its location within the application, but it doesn't matter where I move the CSS files in the application.

Is there a way for me to rewrite what comes out of the url processing or an alternate way of specifying the background image of the DIV without doing any kind of server side processing? Ideally this app will run through the manifest cache feature of HTML5, so I won't have access to any server based languages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

熊抱啵儿 2024-10-09 07:53:45

尝试在这些路径上放置一个前导斜杠来表示根。

即使用:

url('/images/ClanSpider.png')

而不是

url('images/ClanSpider.png')

Try putting a leading slash on those paths to represent the root.

ie use:

url('/images/ClanSpider.png')

instead of

url('images/ClanSpider.png')
流心雨 2024-10-09 07:53:45

通过阅读您对其他答案的评论,我认为您正在为自己制造一个实际上并不存在的问题。如果 url('/images/ClanSpider.png') 在您上传到网络服务器时可以正常工作,那么技巧就是让它在本地工作时以相同的方式工作。到目前为止,这是最简单的方法,特别是如果您的重点是离线应用程序,而服务器端要求很少(我假设这是真的,正如您提到的 file:/// URIs),是运行本地Web服务器。

Python 附带了一个模块 SimpleHTTPServer,如果您已安装了 Python,则开始它就像在命令提示符中转到 L5RHonor 项目目录并发出以下命令一样简单:

python -m SimpleHTTPServer

然后,不要使用这样的 URI 访问文件:

file:///H:/Projects/L5RHonor/images/ClanSpider.png

您将像这样访问它们:

http://localhost:8000/images/ClanSpider.png

所有根相对文件路径现在可以正常工作,作为额外的好处,离线缓存将在 Chrome 中正常工作,您将能够从命令提示符窗口中的日志中看到它正在根据您的清单请求和缓存正确的文件。

From reading through your comments on the other answers I think you're creating a problem for yourself that doesn't really exist. If url('/images/ClanSpider.png') is going to work when you upload to the web server then the trick is to make it work the same way when working locally. By far the easiest way to do this, especially if your focus is an offline app which has little in the way of server side requirements (which I'm assuming is true, as you mentioned file:/// URIs), is to run a local web server.

Python ships with a module SimpleHTTPServer, if you have Python installed then starting it is as simple as going to your L5RHonor project directory in a command prompt and issuing the following command:

python -m SimpleHTTPServer

Then instead of accessing your files with URIs like this:

file:///H:/Projects/L5RHonor/images/ClanSpider.png

You will access them like this:

http://localhost:8000/images/ClanSpider.png

All your root relative file paths will now work correctly, as an added bonus the offline caching will work correctly in Chrome and you'll be able to see from the log in the command prompt window that it is requesting and caching the correct files according to your manifest.

醉生梦死 2024-10-09 07:53:45

最简单的解决方案显然是在 URL 中添加斜杠以使其绝对。这可以正常工作,但无法将应用程序移动到子目录中,或将静态资源移动到不同的服务器。如果这是一个问题,有多种替代方法。

如果可能的背景图像数量是有限的,您可以将每个背景图像定义在其自己的类中:

.bgSpider { background-image: url('images/ClanSpider.png'); }
.bgFalcon { background-image: url('images/ClanFalcon.png'); } 
...

然后执行 .addClass() 来设置正确的图像。

除此之外,据我所知,在Javascript中设置背景图像路径时,没有办法指定相对于样式表(而不是当前文档)的路径。您必须使用绝对路径,或者在 JavaScript 中定义根路径,然后使用它:

// in the script head
imageRoot  = "http://www.example.com/mysite/images";
// later....
$("#MyHonorBox").css('backgroundImage', 'url(' + imageRoot + ClanImage + ')');

The simplest solution is obviously adding a slash to the URL to make it absolute. That will work fine, but it makes it impossible to move the application into a sub-directory, or to move static resources to a different server. If that is a problem, there are various alternative ways.

If the number of possible background images is finite, you could define every one in a class of its own:

.bgSpider { background-image: url('images/ClanSpider.png'); }
.bgFalcon { background-image: url('images/ClanFalcon.png'); } 
...

and then do an .addClass() to set the correct image.

Other than that, as far as I know, there is no way to specify a path relative to the style sheet (rather than the current document) when setting a background image path in Javascript. You would have to work with absolute paths, or define a root path in JavaScript, and use that:

// in the script head
imageRoot  = "http://www.example.com/mysite/images";
// later....
$("#MyHonorBox").css('backgroundImage', 'url(' + imageRoot + ClanImage + ')');
千と千尋 2024-10-09 07:53:45

CSS 文件的位置无关紧要,您正在修改 HTML 元素的 .style 属性。这与使用样式属性相同。

由于这是嵌入在文档中的 CSS,因此所有 URI 都相对于文档

您可能希望以 / 开头 URL,或者如果您确实想要在问题中指定绝对位置:http://

The location of the CSS file is irrelevant, you are modifying the .style property of an HTML element. This is the same as using the style attribute.

As this is CSS embedded in the document, all URIs are relative to the document.

You probably want to start the URL with a /, or if you really want the absolute location specified in your question: http://

半夏半凉 2024-10-09 07:53:45

尝试在 URL 开头添加 / 吗?

Try adding a / at the start of the URL?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文