我試圖做到這一點,當用戶單擊“鏈接”時,它會向下滾動到輸入欄位“a”并切換它而無需單擊。我已經在使用 jQuery,所以也許它也應該處理滾動,但是我不確定如何自動處理切換(也許專注于正確的術語)輸入欄位。注意:為了可讀性,我省略了所有其他代碼。
<a href="#view" class="button">Link</a>
<div id="view">
<input type="text" id="a" name="a">
</div>
uj5u.com熱心網友回復:
不確定“切換”是什么意思,但您可以使用focus將游標(輸入)移動到輸入欄位。這是輸入欄位看不見的片段。處理是使用事件委托完成的。
document.addEventListener(`click`, handle);
function handle(evt) {
if (evt.target.id === `view`) {
// if you click from a href element, you have to prevent
// the default action of the click. May be advisable to
// use a button here.
evt.preventDefault();
const inpVw = document.querySelector(`#a`);
// focusing automatically scrolls to the field
inpVw.focus();
inpVw.setAttribute(`placeholder`, `Feel free to type`);
}
}
#viewInput {
margin-top: 120vh;
}
<a id="view" href="#">Link</a>
<p id="viewInput">
<input type="text" id="a" name="a">
</p>
uj5u.com熱心網友回復:
添加一個空的錨標簽:
<div id="view">
<input type="text" id="a" name="a">
<a name="myawesomeanchour"></a>
</div>
然后將事件處理程式添加到“鏈接”按鈕:
var myawesometag = $("a[name='myawesomeanchour']");
$('html,body').animate({scrollTop:myawesometag.offset().top},'slow');
$('#a').toggle();
如果仍然不需要切換,請移除切換線。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/353260.html
標籤:javascript html 查询
