[英]Setting input to focus using onload
当页面使用onload时,我必须设置价格。
<body style="border: solid 1px black; margin-left: auto;margin-right: auto;width: 500px;">
<form>
<fieldset style="margin:25px";><legend>Discount Calculator</legend>
<div style="text-align: right; margin-right: 25%;">
<label>Enter Price: <input onload = "thing()" type="text" name="price" id="price"></label><br><br>
</div>
</fieldset>
</form>
<script type="text/javascript">
function thing()
{
document.focus()
}
</script>
</body>
我知道我做错了什么。 我可以通过使用document.findElementById("price").focus()
来轻松地将其设置为document.findElementById("price").focus()
,但对于此赋值,我需要将其作为onload进行。 任何帮助,将不胜感激。
如果你正在设置焦点,这是你可以做的。
此外,可能值得阅读这篇文章 ,了解不同的加载事件和含义。
希望这可以帮助。
document.addEventListener("DOMContentLoaded", () => { document.querySelector("#price").focus(); });
<form> <input type="text" name="dummy-1"> <input type="text" name="dummy-2"> <fieldset style="margin:25px" ;> <legend>Discount Calculator</legend> <div style="text-align: right; margin-right: 25%;"> <label>Enter Price: <input onload="thing()" type="text" name="price" id="price"> </label> <br> <br> </div> </fieldset> <input type="text" name="dummy-1"> <input type="text" name="dummy-2"> </form>
像这样的东西:
document.addEventListener( 'DOMContentLoaded', function () {
// Do stuff...
}, false );
它是一个类似的
$( document ).ready(function() {
// Do stuff...
});
用纯Javascript编写而不用jQuery。 还有一个选择是使用
window.onload = function() {
// Do stuff...
}
感谢@mplungjan这个想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.