[英]JavaScript: Get Element by ID and Scroll to Top
我在頁面頂部有一個聯系表,第一個輸入字段的ID為“ input_1_1”。 在內容文本中,有一個“聯系我們”鏈接,該鏈接應跳到頁面頂部並選擇第一個輸入字段,代碼如下所示。
<a href="javascript:document.getElementById('input_1_1').focus()">Contact us</a>
此功能在Chrome瀏覽器中可以完美運行,並向右滾動到頂部,然后選擇第一個字段;在Firefox中,頁面無法向右滾動到頂部,因此,您只能看到第一個輸入字段,而不是表單標題。
如何修改此代碼以滾動到頁面頂部並選擇第一個輸入字段,而不僅僅是選擇第一個輸入字段。
提前致謝
威廉·盧
嘗試這個
<a href="javascript:scrollWin()">Contact us</a>
<script>
function scrollWin()
{
window.scrollTo(0,0);// set the proper x and y coordinates for your contact form.
document.getElementById('input_1_1').focus()
}
</script>
您可以像這樣在鏈接周圍做一個標簽
<label for="input_1_1"><a href="#input_1_1">Contact us</a></label>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.