jQuery加载和相对路径(jQuery loading and relative paths)

我正在使用jQuery加载页面内容,如下所示:

$("#content").load(path);

内容文件纯粹是这样的内容:

<!--page.php--> I am content

但是,当我想在我的内容中包含图像时,如下所示:

<!--page.php--> <img src="images/image.jpg"/>

该路径仅相对于jQuery脚本的原点而不是page.php文件起作用,因此以下示例不适用于上面的代码:

Directory tree: index.php jQuery.js pages/ page.php images/ image.jpg

因为它使page.php相对于jQuery所在的位置,所以它无法找到images文件夹,除非我给它一个确切的路径,在我的实际站点中,所有时间都会根据内容文件的位置而改变。位于。

是否有一种非hacky方式,这不涉及我在我想要引用的每个图像或内容中放置确切的路径?

I'm using jQuery to load page content like so:

$("#content").load(path);

Where the content files are purely content like such:

<!--page.php--> I am content

However, when I want to include images in my content like this:

<!--page.php--> <img src="images/image.jpg"/>

The path only works relative to the origin of the jQuery script instead of the page.php file, so the following example would not work with the code above:

Directory tree: index.php jQuery.js pages/ page.php images/ image.jpg

Because it's making page.php relative where the jQuery is, at the root of the site so it can't find the images folder unless I give it an exact path which in my actual site changes all the time depending on where the content file is located.

Is there a non-hacky way around this that doesn't involve me putting the exact path in every image or content I want to reference?

最满意答案

您可以为映像目录的路径设置一个全局变量。 并将该变量用于路径,无论您将图像放在何处。

如果路径发生更改,则只需更改一个变量值。

You can set one global variable for the path of your image directory. and use that variable for path, wherever you put images.

If your path changes, you need to change only one variable value.

jQuery加载和相对路径(jQuery loading and relative paths)

我正在使用jQuery加载页面内容,如下所示:

$("#content").load(path);

内容文件纯粹是这样的内容:

<!--page.php--> I am content

但是,当我想在我的内容中包含图像时,如下所示:

<!--page.php--> <img src="images/image.jpg"/>

该路径仅相对于jQuery脚本的原点而不是page.php文件起作用,因此以下示例不适用于上面的代码:

Directory tree: index.php jQuery.js pages/ page.php images/ image.jpg

因为它使page.php相对于jQuery所在的位置,所以它无法找到images文件夹,除非我给它一个确切的路径,在我的实际站点中,所有时间都会根据内容文件的位置而改变。位于。

是否有一种非hacky方式,这不涉及我在我想要引用的每个图像或内容中放置确切的路径?

I'm using jQuery to load page content like so:

$("#content").load(path);

Where the content files are purely content like such:

<!--page.php--> I am content

However, when I want to include images in my content like this:

<!--page.php--> <img src="images/image.jpg"/>

The path only works relative to the origin of the jQuery script instead of the page.php file, so the following example would not work with the code above:

Directory tree: index.php jQuery.js pages/ page.php images/ image.jpg

Because it's making page.php relative where the jQuery is, at the root of the site so it can't find the images folder unless I give it an exact path which in my actual site changes all the time depending on where the content file is located.

Is there a non-hacky way around this that doesn't involve me putting the exact path in every image or content I want to reference?

最满意答案

您可以为映像目录的路径设置一个全局变量。 并将该变量用于路径,无论您将图像放在何处。

如果路径发生更改,则只需更改一个变量值。

You can set one global variable for the path of your image directory. and use that variable for path, wherever you put images.

If your path changes, you need to change only one variable value.