html:base 标签

本文详细介绍了HTML中的base标签用法,包括如何为页面中的所有链接指定基础URL,并提供了实例说明。此外,还讨论了该标签在HTML与XHTML之间的区别及使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Definition and Usage
用法

The base element specifies a base URL for all the links in a page.
使用base元素为页面所有的连接指定一个基础的URL


Differences Between HTML and XHTML
在HTML和XHTML中的区别

In HTML the <base> tag has no end tag.
在HTML中<base>标签不需要结束标签

In XHTML the <base> tag must be properly closed.
在XHTML中<base>标签必须合理关闭


Tips and Notes
注意点

Note: The <base> tag must go inside the head element.
注意:<base>标签必须使用在head元素内


Example
实例

Assume that the absolute address for an image is:
假设有一个图片的绝对地址:

<img src="http://www.w3schools.com/images/smile.gif" />

Now we insert the <base> tag, which specifies a base URL for all of the links in a page, in the head section of a page:
现在我们插入<base>标签,就可以为页面内的所有连接加上基础URL:

<head>
<base href="http://www.w3schools.com/images/" />
</head>

When inserting images on the page in the example above, we just have to specify the relative address, and the browser will look for that file using the full URL, "http://www.w3schools.com/images/smile.gif":
加上之后我们只需要指定相对的地址就可以将URL变为"http://www.w3schools.com/images/smile.gif":

<img src="smile.gif" />


Required Attributes
必要属性

DTD indicates in which DTD the attribute is allowed. S=Strict, T=Transitional, and F=Frameset.
下表中的DTD列指明了被支持的DTD属性。S=Strict(精确的),T=Transitional(传统的),F=Frameset(框架)

属性描述DTD
hrefURLSpecifies the URL to use as the base URL for links in the page
为页面的所有连接指定基础URL
STF

Optional Attributes
可选属性

属性描述DTD
target_blank
_parent
_self
_top
Where to open all the links on the page. This attribute can be overridden by using the target attribute in each link.
页面中所有连接的打开方式。如果每个连接使用了target属性那么base的target属性就无效了
  • _blank - all the links will open in new windows
    所有的连接将在新窗口打开
  • _self - all the links will open in the same frame they where clicked
    所有的连接将在自身窗口或是框架中打开
  • _parent - all the links will open in the parent frameset
    所有连接在父级框架集中打开
  • _top - all the links will open in the full body of the window
    所有连接在窗口的主体中打开
TF

Standard Attributes and Events
标准属性以及事件

NONE


Try-It-Yourself Demos
演示

One target for all links
How to use the <base> tag to let all the links on a page have the same target.
怎样使用<base>标签来让所有连接使用同一个target

 

以上是转载!

小弟前几天在做struts的时候发现了小问题(也许别人早发现了@_@),就是当你使用*.do跳转的jsp,他的路径上有问题!如果你有外部应用一个css或者js的时候,要是用*.do跳转的jsp的时候效果就出不来!

这是因为路径的问题!后来小弟终于在网上找到了解决问题的办法,就是使用<html:base>用法看上面教程!

你也可以这样用

在你的action里面先得到你工程的路径!
String basePath  =  request.getScheme()  +   " :// "   +  request.getServerName()
        
+   " : "   +  request.getServerPort()  + request.getContextPath() +    " / " ;
        request.getSession().setAttribute(
" basePath " , basePath);

我是把basePth放到了session里面!然后你的页面就可以这样获得basePath

< base  href ="<%=session.getAttribute(" basePath")% > " />
    
< script  type ="text/javascript"  language ="javascript"  src ="js/all.js" >
    
</ script >

我的js是放在我工程下的一个js文件夹里面的!

小弟还是新人!要是有说的不对的地方!请高手指点!

在这特别感谢(hookee),谢谢你的帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值