简体   繁体   English

如何正确使用document.onload

[英]How to use document.onload Properly

I am currently learning window.onload and document.onload as attributes on html. 我目前正在学习window.onloaddocument.onload作为html的属性。 But i fail to use them properly. 但是我无法正确使用它们。 In my code i want to focus a specific text area named "emri", using the document.onload but without success. 在我的代码中,我想使用document.onload聚焦名为“ emri”的特定文本区域,但没有成功。 Some would say to use it as a script or smth, but i want to use it as an attribute inside the tag. 有人会说将其用作脚本或smth,但我想将其用作标记内的属性。 What am i doing wrong? 我究竟做错了什么?

 <!doctype html>
  <html lang="en">

  <head>
     <meta charset="utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1, 
  shrink-to-fit=no">


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
    crossorigin="anonymous">

<link rel="stylesheet" href="App.css">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ"
    crossorigin="anonymous">


   </head>

   <body onload="document.my-form1.emri.focus();" >

  <main class="my-form">
      <div class="container">
          <div class="row justify-content-flex-start">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">Apliko</div>
                    <div class="card-body">
                        <form name="my-form1" onsubmit=" return formValidation();">
                            <div class="form-group row">
                                <label for="emri" class="col-md-3 col-form-label text-md-right">Emri juaj:</label>
                                <div class="col-md-6">
                                    <input type="text"  class="form-control" name="emri">
                                </div>
                            </div>

Here your updated html : 在这里,您更新的html:

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> <link rel="stylesheet" href="App.css"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous"> </head> <body onload="document.getElementById('emri').focus();"> <main class="my-form"> <div class="container"> <div class="row justify-content-flex-start"> <div class="col-md-6"> <div class="card"> <div class="card-header">Apliko</div> <div class="card-body"> <form name="my-form1" onsubmit=" return formValidation();"> <div class="form-group row"> <label for="emri" class="col-md-3 col-form-label text-md-right">Emri juaj:</label> <div class="col-md-6"> <input type="text" class="form-control" name="emri" id="emri"/> </div> </div> </form> </div> </div> </div> </div> </div> </main> </body> </html> 

您可以仅将autofocus属性添加到输入中。

<input type="text"  class="form-control" name="emri" autofocus>

There is nothing wrong how you use `window.onload´. 您使用`window.onload'没错。 It is the name of the form. 它是表单的名称。 Since there is a "-" character in the name, you should use it like this: 由于名称中有一个“-”字符,因此您应该这样使用它:

<body onload="document['my-form1'].elements.emri.focus();">

see: Example 请参阅: 示例

You need to select like this 您需要选择这样

<body onload="document.getElementsByName('emri')[0].focus();">

If you are using HTML5, you can use like autofocus attribute in input . 如果您使用的是HTML5,则可以在input使用like autofocus属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM