一直在努力解决这个问题,因为我是JS的新手。我想要实现的目标:
我的代码看起来像这样。
FIDDLE 的 HTML
<section>
<p>Lorem<a id="fn1-ref" href="#fn1" title="link to footnote" class="art-fn-ref"><sup>1</sup></a>.</p>
<p>ipsum<a id="fn2-ref" href="#fn2" title="link to footnote" class="art-fn-ref"><sup>2</sup></a></p>
<p>dolor<a id="mulfn1-1-ref" href="#fn3" title="link to footnote" class="art-fn-ref"><sup>3</sup></a>.</p>
<p>sit<a id="mulfn1-2-ref" href="#fn3" title="link to footnote" class="art-fn-ref"><sup>3</sup></a>.</p>
</section>
<aside>
<ul>
<li><a id="fn1" href="#fn1-ref" title="link to main text" class="art-fn"><sup>1</sup></a> Footnote 1</li>
<li><a id="fn2" href="#fn2-ref" title="link to main text" class="art-fn"><sup>2</sup></a> Foonote 2</li>
<li><a id="fn3" href="#mulfn1-1-ref" title="link to main text" class="art-fn"><sup>3</sup></a> Foonote 3</li>
</ul>
</aside>
JS
//convention
//ID of reference to footnote= Caller ID
//ID of the footnote= FN ID
//HREF of the footnote that refer back to the caller ID = FN HREF
$(document).ready(function () {
$(".art-fn-ref").one('click', function (e) {
//001 - check if the Caller ID match a pattern (mulfn#)
$('[id^=mulfn]').filter(function () {
//002 - copy the 001 ID
//003 - store the original HREF and ID of the footnote (FN HREF and FN ID)
//004 - replace the FN HREF with HREF that points out to the correct caller ID
//005 - once the "modified" FN HREF is clicked, restore it back to the original FN HREF
});
});
});
例如。脚注3有两个引用,其中包含以下ID&#34; mulfn1-1-ref&#34; &安培; &#34; mulfn1-2-REF&#34 ;.假设我点击了&#34; mulfn1-2-ref&#34;它把我带到了&#34; fn3&#34;如果我点击fn3,我需要它带我回到&#34; mulfn1-2-ref&#34;而不是&#34; mulfn1-1-ref&#34;。
怎么做?请帮忙。
答案 0 :(得分:0)
点击其中一个顶部链接
data-href
属性和引荐链接的ID click
事件侦听器,以禁用默认操作并将window.location
设置为data-href
值,然后删除事件侦听器
$(document).ready(function () {
$(".art-fn-ref").on('click', function (e) {
var id = $(this).attr('id'),
$target = $($(this).attr('href'));
$target.attr('data-href','#'+id);
$target.on('click',function(e) {
e.preventDefault();
window.location = $(this).attr('data-href');
$(this).off('click');
})
});
});
&#13;
html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
blockquote,
pre,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
display: block;
}
body {
line-height: 1;
}
ol,
ul {
list-style: none;
}
blockquote,
q {
quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
content: "";
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
.art-fn-ref {
display: inline-block;
text-decoration: none;
background-color:#CCC;
border: 1px solid #555;
padding: 1px;
vertical-align: super;
font-size: 75%
}
a {
}
.art-fn {
display: inline-block;
text-decoration: none;
background-color:#4CAF50;
border: 1px solid #555;
padding: 1px;
font-size: 75%
}
p {
padding: 6px 0 3px 0;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<p>Lorem<a id="fn1-ref" href="#fn1" title="link to footnote" class="art-fn-ref"><sup>1</sup></a>.</p>
<p>ipsum<a id="fn2-ref" href="#fn2" title="link to footnote" class="art-fn-ref"><sup>2</sup></a></p>
<p>dolor<a id="mulfn1-1-ref" href="#fn3" title="link to footnote" class="art-fn-ref"><sup>3</sup></a>.</p>
<p>sit<a id="mulfn1-2-ref" href="#fn3" title="link to footnote" class="art-fn-ref"><sup>3</sup></a>.</p>
</section>
<aside>
<ul>
<li><a id="fn1" href="#fn1-ref" title="link to main text" class="art-fn"><sup>1</sup></a> Footnote 1</li>
<li><a id="fn2" href="#fn2-ref" title="link to main text" class="art-fn"><sup>2</sup></a> Foonote 2</li>
<li><a id="fn3" href="#mulfn1-1-ref" title="link to main text" class="art-fn"><sup>3</sup></a> Foonote 3</li>
</ul>
</aside>
&#13;