簡體   English   中英

如何使用javascript導航到同一頁面中的HTML元素...

[英]How to navigate to the HTML element in the same page using javascript…

我想在點擊按鈕時使用javascript導航到同一頁面中具有特定“id”的HTML元素。

例如:

<body>
   <div id="navigateHere" >
   </div>

   <button onclicK="navigate();" />

在上面的代碼中,javascript函數navigate()中應該有什么,這樣點擊一個按鈕,它將導航到id為'navigateHere'的'div'元素.....

提前致謝 ...

 window.location = "#navigateHere";

您可以使用簡單的鏈接代替按鈕:

<div><a href="#navigateHere">Link text</a></div>

如果您需要使用JavaScript和按鈕,則以下內容應該有效:

HTML:

<button type="button" id="someid">Link text</button>

JavaScript的:

document.getElementById("someid").onclick = function () {
    window.location.hash = "#navigateHere";
};

它將導航到div元素......

這可能就是你想要的。

window.location.hash = 'navigateHere';

但是沒有必要使用JavaScript。 只需鏈接到#navigateHere

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM