markdown 修复您的跳过链接。 [JS]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 修复您的跳过链接。 [JS]相关的知识,希望对你有一定的参考价值。
(function(a,b,c,d){for(c in a)(d=a[c].hash)&&a[c].href==b+d&&a[c].addEventListener&&a[c].addEventListener("click",function(a,b,c,d){if(a=(b=document).getElementById(c=this.hash.slice(1))||b.getElementsByName(c)[0])(d=!a.getAttribute(b="tabindex"))&&a.setAttribute(b,-1),a.focus(),d&&a.removeAttribute(b)})})(document.links,location.href.split("#")[0]);
(function(a,b,c,d) {
for (c in a) {
if ((d=a[c].hash)&&a[c].href==b+d&&a[c].addEventListener) {
a[c].addEventListener('click',function(e,f,g,h) {
if (e=(f=document).getElementById(g=this.hash.slice(1))||f.getElementsByName(g)[0]) {
if (h=!e.getAttribute(f='tabindex')) e.setAttribute(f,-1);
e.focus();
if (h) e.removeAttribute(f);
}
});
}
}
})(document.links,location.href.split('#')[0]);
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
## Fixing your skip links.
[Read Damon Muma on this.](http://whatisdamon.com/blog/?p=247 'Why your ‘Skip to Content’ link might not work') He proposes the following jQuery solution (inspired by [Thompson](http://terrillthompson.com/blog/161 'Terrill Thompson'), fixed by me):
```javascript
// Apply focus properly when accessing internal links with keyboard in WebKit browsers.
$("a[href^='#']").not("a[href='#']").click(function() {
$("#"+$(this).attr("href").slice(1)+"").focus();
});
```
I don’t like to use jQuery however for things that should be seen as required code for any website. Below I include my own JavaScript port, including some error checking and more complete `HREF` support.
### Version history.
1. Pilot version. ([d385d2](https://gist.github.com/1900563/d385d26f9903106ba31c3340bac8301508b98d4e))
2. `tabindex` now using `-1` instead of `0`, as per [Smith](http://terrillthompson.com/blog/161#comment-828 'Jared Smith'), added this README, and added UNLICENCE. ([c194df](https://gist.github.com/1900563/c194df813181bd8c101a27167f82e257668e58fa))
3. Updates to the README. (prev. [0fce31](https://gist.github.com/1900563/0fce31a7685985a1b90f487ba07e9cdb224f5ba6)) (prev. [a31368](https://gist.github.com/1900563/a31368a0191657b0b6e3421b9f4eec31780cb06a)) ([7d175b](https://gist.github.com/1900563/7d175b2bb4a1bd8710399114592bf02e2fba72d6))
4. Added a clause to check for pre-existing `tabindex` values. ([a39c9b](https://gist.github.com/1900563/a39c9bf36966edc74c80af5f8cf0c534733d4fbb))
5. Added support for named anchors. The `name` attribute is obsolete and is only checked for backwards compatibility. ([0ab22c](https://gist.github.com/1900563/0ab22c09b9ee2c356cb5a5a210ba01c95abcfb19))
6. No more errors on empty fragments (`#`) or undefined elements (`#thisIDisnotused`). ([9ebb30](https://gist.github.com/1900563/9ebb30db4da9f305dc453b119208acc4a6efa976))
7. Added support for URLs not starting with `#` but starting with the current page’s location. Also updated the README with a list of tested browsers. ([8adf2a](https://gist.github.com/1900563/8adf2ae94d7df881480214c1a302f5c5d5348a40))
8. Shaved off some bytes and added a compressed version. ([116262](https://gist.github.com/1900563/116262460d9a1f36b29f907b9a06e5dde22d54b6))
9. Updated the README, it is no longer just a quick port. ([924088](https://gist.github.com/1900563/9240882e690ed8194daefd43e4547a1c583bfeb7))
10. Even smaller. (prev. [f6cf48](https://gist.github.com/1900563/f6cf4862e5de2bcef59bd19ebe7b5e64fd5fb1bf)) ([aeb909](https://gist.github.com/1900563/aeb90997291b8557fcf3049d01b5796489917e12))
11. Fix for pages using a `BASE` element. As reported by [Gartner](http://whatisdamon.com/blog/?p=247&cpage=1#comment-772 'Martin Gartner'). (prev. [b8648a](https://gist.github.com/1900563/b8648a196069ccb91397b5997524b591b2ffe337)) ([341fdf](https://gist.github.com/1900563/341fdf7850470d6e18bbe8a66d1866fe0cf0b42f))
12. Stop overwriting existing `onClick` events. ([376b75](https://gist.github.com/1900563/376b7544be3d8ef4ab74a43bcd969e33a3262fe8))
13. Inspired by [Duffy’s](http://whatisdamon.com/blog/?p=247&cpage=1#comment-781 'Paul Duffy') take on things it now uses `addEventListener` instead of `onClick` to avoid event clashes, and the complete `location` value instead of a virtual `A`-element. Also fine-tuned it to not apply to too many wrong links and shortened the code some more. ([05bff0](https://gist.github.com/1900563/05bff0a4189418a6b6e966bc1512e1f84b16d475))
14. **Current:** Saving 6 more bytes on [skiplinks.min.js](#file_skiplinks.min.js) via Google’s [Closure Compiler Service](https://closure-compiler.appspot.com/). (prev. [e0ca59](https://gist.github.com/1900563/e0ca595d4a7a02ee94d39c4fcd3ddb2bf5e1ed0f))
### Tested browsers
* Firefox 10.0.2
* Chrome 17.0.963.56
* Safari 5.0.6 (5533.22.3)
以上是关于markdown 修复您的跳过链接。 [JS]的主要内容,如果未能解决你的问题,请参考以下文章
基于 Koa平台Node.js开发的KoaHub.js的跳过组件代码