2
我用e.preventDefault();
禁用滚动页面无效input
。我试了scrollintoview(false)
,默认情况下滚动网站顶部,但它什么都没有做。显示需要的输入弹出(泡泡)
现在,滚动页面无效input
,我使用我自己的函数,在屏幕上的中心元素。
不幸的是,通过这种方法,我还禁用了警告气泡,并在无效元素上禁用了focus
。触发重点并不难 - input.focus();
。但我应该怎么做,以显示输入错误气泡通过JS?出于某种原因,我不能使用form.reportValidity()
函数。我需要这样的东西input.triggerError();
// JS code, to center site on element:
// event.preventDefault();
// var elOffset = element.getBoundingClientRect().top + window.scrollY;
// var elHeight = element.height();
// var windowHeight = window.innerHeight;
// var offset;
// if (elHeight < windowHeight) {
// offset = elOffset - ((windowHeight/2) - (elHeight/2));
// }
// else {
// offset = elOffset;
// }
// window.scrollTo(0, offset);
// element.focus();
// I CAN'T DO THIS:
// FORM.reportValidity();
// I'm searching for sth like this:
// element.reportValidity();
body {
margin-top: 70px;
}
.fixed-menu h1,
.fixed-menu p {
color: #ffffff;
margin: 0;
}
.fixed-menu {
background-color: #222;
border-color: #080808;
top: 0;
border-width: 0 0 1px;
position: fixed;
right: 0;
left: 0;
z-index: 1030;
height: 70px;
}
<div class="fixed-menu">
<h1>This is fixed menu</h1>
<p>I'm using e.preventDefault(); to disable that activity</p>
</div>
<form>
<br>
<label>this is input:
<input type="text" required>
</label>
<p style="margin-bottom: 100px"><b>Tip:</b><br>
1. leave this input, and srcoll to submit button.<br>
2. click on it.<br>
3. see - scroll is not good enought</p>
<button type="submit">Submit form</button>
</form>
调用它异步这么说,你的主要目标是[表明输入框]包含[点击'提交表单']时的'无效数据'? – eeya
@eeya是的,但我认为这是完成的。如果你看上面的JS代码,你会看到我计算了“中心”的位置,脚本可以滚动到它们并聚焦输入。问题是从输入弹出/泡沫/工具提示 - 它不显示:/ –