基本标签搞乱书签链接

时间:2009-01-02 01:13:06

标签: html hyperlink

我有一个使用mod_rewrite创建漂亮网址的Joomla网站。

http://www.example.com/resources/newsletter

然而这造成了一个问题。包括这样的图像:src="images/pic.jpg",然后它将在以下位置查找文件:

http://www.example.com/resources/newsletter/images/pic.jpg

......显然不存在。要解决此问题,我在<base>部分中添加了head标记:

<base href="http://www.example.com/" />

...工作正常,直到我尝试在同一页面上链接到锚点(书签):

<!-- on http://www.example.com/resources/newsletter -->
<a href="#footer">go to the footer</a>

<!-- clicking that link takes you to http://www.example.com/#footer -->

将我的链接更改为<a href="resources/newsletter/#footer">是不可行的,因为编辑时我不一定知道该网页的网址。有没有办法让一些链接忽略<base>指令?

虽然我真的更喜欢直接的HTML解决方案,但我已经在这个网站上使用了jQuery,所以如果我被困住,这可能是一个选择。

2 个答案:

答案 0 :(得分:2)

是否可以将src属性更改为/images/pic.jpg?这将达到您正在寻找的效果。

如果那不可能,这个(未经测试的)jQuery代码应该适合你:

$('a[@href^="#"]').click(function() { 
  var hash = this.hash, el = $(hash), offset;
  if(!el.size()) {
    el = $("[@name=" + hash.slice(1) + "]");
  }

  offset = el.offset();
  window.scroll(offset.left, offset.top);
}); 

答案 1 :(得分:0)

老问题,新答案.. 试试这个:

$('a[href^="#"]').on('click', function (event) { 
    event.preventDefault();
    window.location.hash = $(this).attr('href');
});