为什么基本标签不适用于相对路径?

2023-07-31前端开发问题
1

本文介绍了为什么基本标签不适用于相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在页面的 <head> 部分有一个 <base> 标记,如下所示:

I have a <base> tag as below in <head> section of the page:

<base href="http://localhost/framework">

下面是一个相对的脚本(当然在<base>之后):

And a script as below which is relative (of course after <base>):

<script src="/assets/jquery-1.7.1.min.js">

但是当我从 firebug 打开 jQuery 时,它显示:

But when I open jQuery from firebug it shows:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body> 
Blah Blah Blah....

当我使用下面的链接时没关系:

When I use the below link it's OK though:

<script src="http://localhost/framework/assets/jquery-1.7.1.min.js">  

我到处寻找答案,但看来我的工作做得对!那么问题出在哪里?

I looked for an answer everywhere, but it seems I'm doing my job right! So what is the problem?

推荐答案

/assets/jquery-1.7.1.min.js 不是相对的而是绝对的*,/ 即使使用 base 标记也将其带到根目录.

/assets/jquery-1.7.1.min.js is not relative but absolute*, the / takes it to the root even with a base tag.

如果您删除该 /,它应该使其相对于当前路径,当 base 标记存在时,该路径将是 http://本地主机/框架/.

If you remove that /, it should make it relative off the current path, which, when a base tag is present would be http://localhost/framework/.

注意:您还需要在 href 的末尾添加一个尾随 /,以表明它是一个文件夹.

Note: You will also need to add a trailing / to the end of the href, to indicate that it's a folder.

完整的工作示例:

<!doctype html>
<html>
<head>
<base href="/test/" />
<script src="assets/test.js"></script>
<body>
hi
</body>
</html>

* 实际上取决于你问的是谁,它仍然是相对的,因为它与当前域无关.但我更喜欢称它为绝对,因为它表示路径来自根,基于当前域.虽然,我想从技术上讲,这使得它在宏伟的计划中是相对的,而仅就当前领域而言是绝对的.随便.

* Actually depending on who you ask, it's still relative since it is relative off the current domain. But I prefer to call this absolute since it's signifying the path is from the root, based on the current domain. Although, I guess technically that makes it relative in the grand scheme of things, and absolute only in terms of the current domain. Whatever.

这篇关于为什么基本标签不适用于相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13

getFullYear 在一年的第一天返回前一年
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20 前端开发问题
6