2016-11-18 128 views
1

我試圖使用重新加載頁面去錨,但頁面不重新加載或滾動到錨點。直到鏈接被擊中,錨點才顯示。ID錨問題

$('.link').click(function() { 
 
    $('#box').css('display', 'block'); 
 
});
#box { 
 
    display: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="box"> 
 
    <h1>Title</h1> 
 
    <p>Description of the content.</p> 
 
</div> 
 
<a href="#box" class="link">Go to #Box</a>

這是在我的博客頁面:http://simulatorio.blogspot.com.br/p/politicas.html#cookies

對於爲例:

  1. 我在一個小窗口中打開http://simulatorio.blogspot.com.br/p/politicas.html(共500像素寬,內容是示出)。
  2. 我點擊菜單(內容隱藏,菜單顯示)。
  3. 我點擊鏈接http://simulatorio.blogspot.com.br/p/politicas.html#cookies
  4. 該頁面什麼都不做(它不會重新加載)!

我想要的是自己去頁http://simulatorio.blogspot.com.br/p/politicas.html#cookies

PS .:只有在新窗口/選項卡中打開鏈接時纔有效。

它應該在小屏幕上打開,因爲問題在那裏發生(響應頁面)。謝謝!

+0

您使用'顯示:無;'爲你的目標導航到的元素。顯示屬性'none'就好像它所應用的元素不存在於文檔中一樣,因此可以導航/滾動到。 –

+0

@BekimBacaj你認爲我可以在同一頁面重新打開它嗎?例如:我在頁面'http:// simulatorio.blogspot.com.br/p/politicas.html'(#cookies正在顯示)。我打開菜單(#cookies被隱藏)。我點擊鏈接(然後點擊'http:// simulatorio.blogspot.com.br/p/politicas.html#cookies')。 –

+2

抱歉,但我無法理解場景以及在沒有演示的情況下使其發生所需的底層代碼。但乾淨的切割是使用可見性屬性來隱藏該元素,而不是隱藏元素。否則,不要改變其顯示並隱藏溢出;同時設置高度:0px;並在需要顯示時將其刪除。 –

回答

2

如果錨是display:none,它可能不存在導航的目的。試試這個造型來代替:

#box { 
    width:0; 
    height:0; 
    overflow:hidden; 
} 

或者:

#box { 
    visibility:hidden; 
} 
+0

我可以看到它的工作方式。但是你認爲我可以在同一頁面打開這個鏈接:http:// simulatorio.blogspot.com.br/p/politicas.html#cookies'(http://simulatorio.blogspot.com.br/p/ politicas.html#cookies')再次打開頁面。 –

+0

@PauloDosSantos設置位置後,你可以執行'location.reload()'強制頁面重新加載。我不確定它是否會導航到正確的位置。如果位置實際上沒有改變,瀏覽器通常會保持當前的滾動狀態。 – Ouroborus

+0

是的,我試圖避免目前的滾動。不幸的是,我的設計都是連接在一起的。如果我使用'width:0; 身高:0; overflow:hidden;'或 'visibility:hidden;'我需要從零重建我的整個博客。 –

1

這是因爲刷新後風格應用於#box所以其隱藏。

#box { 
    display: none; 
} 
+0

是的,我知道!我想我需要改進我的目標。你認爲我可以在同一頁面重新打開它嗎?例如:我在頁面「http:// simulatorio.blogspot.com.br/p/politicas.html」(正在顯示#box)。我打開菜單(#box已隱藏)。我點擊鏈接(然後點擊'http:// simulatorio.blogspot.com.br/p/politicas.html#box')。 –

+0

您可以通過使用查詢字符串來實現此目的。像 –

+1

http://simulatorio.blogspot.com.br/p/politicas.html?q=#box –

1

你需要的是這樣的:

HTML:

<div id="box" class="boxContent"> 
    <h1>Title</h1> 
    <p>Description of the content.</p> 
</div> 
<a href="#box" class="link">Go to #Box</a> 

CSS:

.boxContent { 
    visibility:hidden; 
} 

JS:

$('.link').click(function() { 
    $('#box').toggleClass("boxContent"); 
}); 

FIDDLE:http://jsfiddle.net/2TCy4/42/

+0

謝謝!我真的很感激。不幸的是,這種方法似乎不適合我的概念。我解決了使用jquery,我已經運行菜單/內容系統。 –

+1

@PauloDosSantos沒問題,可能會在將來用不同的場景爲你使用.. – rJ7

+0

是的!非也!謝謝!!! –