jQuery focus without scroll

前端 未结 7 2213
时光取名叫无心
时光取名叫无心 2020-12-08 13:48

How can I focus to a HTML element (ex. \"a\") and do not change the current scroll settings.

For ex. if I use:

$(\'#link\').focus();
<
7条回答
  •  执笔经年
    2020-12-08 14:20

    Use {preventScroll: true} option on the focus method. https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus

    If using jQuery try $('#el')[0].focus({preventScroll: true}) or iterate over each in your collection

提交回复
热议问题