我正在使用JSDoc。它会生成带有句点的ID,例如:
<a id=".someMethodName"></a>
如果页面的其他部分有
<a href="#.someMethodName"></a>
这非常完美。单击第二个锚点将滚动到第一个。
但是,无论document.querySelector
还是jQuery都找不到该锚点。
为什么浏览器本身接受此锚点而jQuery和querySelector不接受?
test("document.querySelector('#.someMethodName')", function() {
document.querySelector('#.someMethodName');
});
test("$('#.someMethodName')", function() {
$('#.someMethodName');
});
function test(msg, fn) {
try {
var result = fn();
log(msg, result);
} catch(e) {
log(msg, e);
}
}
function log() {
var pre = document.createElement("pre");
pre.appendChild(document.createTextNode(Array.prototype.join.call(arguments, " ")));
document.body.appendChild(pre);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#.someMethodName">click here to go to anchor and see errors</a>
<pre>
put
some
text
here
so
the
page
is
long
enough
that
when
we
click
the
anchor
the
browser
has
as
a
place
to
scroll
that
is
off
screen
otherwise
we'd
have
no
way
to
see
if
it
worked
or
not
</pre>
<a id=".someMethodName">we should scroll to here</a>
<p>did we make it?</p>
<hr/>
'#\\.someMethodName'
- Arun P Johny