【发布时间】:2016-04-07 12:10:32
【问题描述】:
我正在更新这个超级古老的医疗网站,并试图找出一种方法来处理数百个 href,每个 href 都有一个文档和一个 #hash 值,就像这样......
<a href="mind.html#cretinism" target="_top">CRETINISM</a>
<a href="brain.html#delirium" target="_top">DELIRIUM</a>
<a href="body-mind.html#dementia" target="_top">DEMENTIA</a>
<a href="body.html#emotions" target="_top">EMOTIONS</a>
<a href="chem.html#fears" target="_top">FEARS</a>
我正在尝试做的是保留哈希值,并摆脱文档,请参阅。因此,我想最终是这样的:
<a href="#cretinism" target="_top">CRETINISM</a>
<a href="#delirium" target="_top">DELIRIUM</a>
<a href="#dementia" target="_top">DEMENTIA</a>
<a href="#emotions" target="_top">EMOTIONS</a>
<a href="#fears" target="_top">FEARS</a>
或者,可选地,使其 onclick 忽略文档并仅响应哈希值。如,
// pseudo code, obviously
$(document).on('click', 'a', function(e){
e.preventDefault();
$(this).attr('href').sliceOffEveryCharBeforeThe('#');
});
哇哦。
【问题讨论】: