Html基本标记打破了#reference

时间:2015-06-03 20:21:06

标签: angularjs html5 base meanjs

我正在创建一个带有角度的HTML5应用程序。为了工作,我需要在html标题中添加基本URL。

<base href="/">

然后当我点击脚注时,它不会重定向到正确的网址。

http://micd.herokuapp.com/#fnref2:1

而不是

http://micd.herokuapp.com/articles/556acc58cbf0d10b000be0c8#fnref2:1

我想知道你是否有任何线索可以解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分:1)

我正在使用markdown-it,所以我通过在每个链接中添加路径名来编辑规则。

var md = window.markdownit()
    .use(window.markdownitFootnote);

md.renderer.rules.footnote_ref = function (tokens, idx) {
  var n = Number(tokens[idx].meta.id + 1).toString();
  var id = 'fnref' + n;
  var uri = window.location.pathname;
  if (tokens[idx].meta.subId > 0) {
    id += ':' + tokens[idx].meta.subId;
  }
  return '<sup class="footnote-ref"><a href="' + uri + '#fn' + n + '" id="' + id + '">[' + n + ']</a></sup>';
};

md.renderer.rules.footnote_anchor = function(tokens, idx) {
  var n = Number(tokens[idx].meta.id + 1).toString();
  var id = 'fnref' + n;
  var uri = window.location.pathname;
  if (tokens[idx].meta.subId > 0) {
    id += ':' + tokens[idx].meta.subId;
  }
  return ' <a href="' + uri + '#' + id + '" class="footnote-backref">\u21a9</a>'; /* ↩ */
};

非特定解决方案:

$(document).ready(function () {
    var pathname = window.location.pathname;
    $('a').each(function () {
       var link = $(this).attr('href');
       if (link.substr(0,1) == "#") {
           $(this).attr('href', pathname + link);
       }
    });
}
相关问题