<base> 标签为页面上的所有链接规定默认地址或默认目标。
base 标签的 href 属性规定页面中所有相对链接的基准 URL。
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no"> <title>新建H5模板</title> <base href="http://onestopweb.cn/test/img/" target="_self"> </head> <body> <img src="o_s017.jpg" alt="根据base获取图片"/><!--结果是:http://onestopweb.cn/test/img/o_s017.jpg--> <img src="/test/img/o_s018.jpg" alt="站点的绝对路径获图片"><!--结果是:http://onestopweb.cn/test/img/o_s018.jpg--> <a href="demo.html">测试链接</a><!--结果是:http://onestopweb.cn/test/img/demo.html--> <a href="/test/demo.html">测试链接</a><!--结果是:http://onestopweb.cn/test/demo.html--> <div style="background:url(o_s021.jpg) no-repeat; width:200px; height:150px;"></div><!--结果是:http://onestopweb.cn/test/img/o_s021.jpg--> <div style="background:url(/test/img/o_s019.jpg) no-repeat; width:200px; height:150px;"></div><!--结果是:http://onestopweb.cn/test/img/o_s019.jpg--> </body> </html>
效果图:
提示:要把HTML的结构写完整,否则在手机的浏览器上无法显示图片。