0

有没有一种简单的方法可以将属性添加data-scroll到 kramdown 脚注。

[Battlezone](www.github.com) [^1]

我上面的脚注,呈现如下:

<sup id="fnref:1"><a href="#fn:1" class="footnote">1</a></sup>

是否可以将那个data-scroll属性添加到锚标签?

4

2 回答 2

0

我不确定您实际上想在data-scroll属性中写什么。但这里有一个示例 JS,它将这些属性添加到这些链接。将此代码放在结束</body>标记之前。

您想要的是获取每个脚注链接(仅返回内容的链接),然后为所有这些添加属性。这可以这样做:

仅限 JS

var footnoteLinks = document.getElementsByClassName("reversefootnote");
var i, hrefAttribute;
for (i = 0; i < footnoteLinks.length; i++) {
    hrefAttribute = footnoteLinks[i].getAttribute("href");
    footnoteLinks[i].setAttribute("data-scroll", hrefAttribute);
}

jQuery

如果你碰巧有可用的 jQuery,这更容易做到:

$('.reversefootnote').each(function(){
    var footNoteLink = $(this);
    footNoteLink.attr("data-scroll", footNoteLink.attr("href"));
});

如果你想将data-scroll值设置为一个值(例如 all to "true"),代码变得更加简单:

// JS only
var footnoteLinks = document.getElementsByClassName("reversefootnote");
var i;
for (i = 0; i < footnoteLinks.length; i++) {
    footnoteLinks[i].setAttribute("data-scroll", "true");
}
// jQuery
$('.reversefootnote').each(function(){
    footNoteLink.attr("data-scroll", "true");
});

如果这不是您想要的,那么这肯定会给您一个提示。

于 2015-03-15T15:10:43.110 回答
0

您可以将属性添加到脚注定义中的单个脚注。

This is some text with a footnote.[^note]

[^note]:{: .class #id other-attribute="attribute"}
    This footnote has some attributes.

输出:

<p>This is some text with a footnote.<sup id="fnref:note"><a href="#fn:note" class="footnote">1</a></sup></p>

<div class="footnotes">
    <ol>
        <li id="fn:note">
            <p class="class" id="id" other-attribute="attribute">This footnote has some attributes. <a href="#fnref:note" class="reversefootnote">&#8617;</a></p>
        </li>
    </ol>
</div>
于 2015-03-10T23:00:36.160 回答