嗨我有一個頁面,使用錨點進行導航。加載時,索引頁面(A)需要跳轉到錨點。很好,我可以在加載函數中做到這一點,但從頁面B回到頁面A我想跳轉到不同的錨點。我有這樣的工作:去負載錨點(如果其他語句)
<!-- go to Anchor -->
<script type="text/javascript">
function goToAnchor() {
var urllocation = location.href;
if(urllocation.indexOf("#top") > -1){
window.location.hash="top";
} else {
window.location.hash="middle";
}
}
</script>
#middle
是不包含錨鏈接來(或鍵入地址到地址欄)時使用的錨。我想添加三個'如果'。如果我正在做一些愚蠢的事情,我對js很新,所以對我很感興趣。我試過這個:
function goToAnchor() {
var urllocation = location.href;
if(urllocation.indexOf("#right") > -1){
window.location.hash="right";
} else {
window.location.hash="middle";
}
if(urllocation.indexOf("#bottom") > -1){
window.location.hash="bottom";
} else {
window.location.hash="middle";
}
if(urllocation.indexOf("#left") > -1){
window.location.hash="left";
} else {
window.location.hash="middle";
}
但不是快樂,js打破了,不再去頁面加載#middle。
我嘗試了外卡的方法:
function goToAnchor() {
var urllocation = location.href;
if(urllocation.indexOf("#.") > -1){
window.location.hash=".";
} else {
window.location.hash="middle";
}
}
同樣,沒有喜悅。
任何幫助,請..謝謝好心
@朱利安D. 完美的,通配符的方法,因爲我想要的。我知道這很簡單,我正在聚集。至於如果其他方法,我從來沒有想過這個... 感謝所有的快速答覆。 – vincentieo 2012-04-10 16:55:36