使用 jQuery load 函数作为绝对路径

发布于 2024-10-27 12:46:47 字数 818 浏览 1 评论 0原文

我有几个页面,这些页面可以直接访问,也可以从 iFrame 访问。页面可以作为 iFrame 加载的部分用于我需要的某些功能。

问题是,当我在 jQuery 中使用 .load() 函数时,找不到样式表、脚本、一些图像等,因为 .load() 尝试将所有内容作为调用位置的相对路径加载。

我需要像快照一样加载 iFrame 中的页面,这意味着它们的行为方式应该与直接访问它们的方式相同。

试图总结一下,这就是问题所在:

文件的位置如下:

http://localhost/www/folders/myfolder/myfile.html

调用 .load() 函数的索引文件位于根目录中,即

http://localhost/www/index.php

myfile.html 包含如下链接:

css/style.css
js/script.js

那里,这些位于:

http://localhost/www/folders/myfolder/css
http://localhost/www/folders/myfolder/js

因此,当 .load() 函数从根文件夹加载文件时,找不到这些文件,因为它们的读取方式如下:

http://localhost/css/ instead of http://localhost/folders/myfolder/css

我怎样才能解决这个问题?

非常感谢

I have several pages and these pages can be accessed either directly, or from an iFrame. The part where the page can be loaded as an iFrame is used for some functionality that I need.

The problem is, that when I use the .load() function in jQuery, the stylesheets, scripts, some images, etc are not being found because the .load() tries to load everything as a relative path from where it is called.

I need to load the pages in the iFrame kind of like a snapshot, which means that they should behave the same way as if they were accessed directly.

Trying to summarize, this is the problem:

The file is located as follows:

http://localhost/www/folders/myfolder/myfile.html

The index file from where the .load() function is called is in the root, i.e.

http://localhost/www/index.php

myfile.html contains links like:

css/style.css
js/script.js

There, these are located at:

http://localhost/www/folders/myfolder/css
http://localhost/www/folders/myfolder/js

Therefore, when the .load() function loads the file from the root folder, these are not found because they are read as follows:

http://localhost/css/ instead of http://localhost/folders/myfolder/css

How can I get around this?

Many thanks

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

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

发布评论

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

评论(3

匿名。 2024-11-03 12:46:47

您可以使用 jQuery 将 基本标签 添加到加载的页面,

      $("head").append("<base href="http://localhost/folders/myfolder/" />");

具体取决于您应该播放的加载顺序有了这个。 (我不确定CSS是否需要在设置之前或之后加载)

you could add a base tag to your loaded page with jQuery

      $("head").append("<base href="http://localhost/folders/myfolder/" />");

depending on the loading order you should play with this. (I am not sure if the css needs to be loaded BEFORE or AFTER this has been set)

抱猫软卧 2024-11-03 12:46:47

我想你可以试试这个,

<?php
define('file_path_css','folder/myfolder/css');

include file_path_css;

?>

这可能有用..

I think you can try this,

<?php
define('file_path_css','folder/myfolder/css');

include file_path_css;

?>

this might work..

贩梦商人 2024-11-03 12:46:47

听起来像在 myfile.html 中,您只需将“folder/myfolder/”添加到样式表和脚本路径中。

Sounds like in your myfile.html, you just need to add "folder/myfolder/" to your stylesheet and script paths.

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