提问者:小点点

搜索框中的Javascript


我有html javascript显示我今天的日期和一个搜索框。我尝试创建html,以便在搜索框中始终规定当前日期。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Titel</title>
    </head>
    <body>
                 <script>           var datum = new Date(); // aktuální datum
                var retezec = ""; // postupně se k němu budou přičítat další řetězce
                retezec += datum.getDate() + ". "; // Den v měsíci
                retezec += (1 + datum.getMonth()) + ". "; // Měsíce jsou číslovány od nuly
                document.write( retezec ); // Výpis řetězce do dokumentu</script>

 <a href="https://translate.google.com/">
    <img class="logo-search" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" style="position: absolute; left:45%" width="10%" height="10%"></a>
    <form class="search-form" method="get" action="https://translate.google.com/" target="_top"; style="position: absolute; left:250pt; top: 30%">
        <input type="text" value = retezec name="q" class="search-bar" style="font-size: 30pt; width: 150%; font-family: 'Times New Roman', Times, serif;">
    </form>
    

    </body>
</html>

共1个答案

匿名用户

您需要设置搜索输入的值,并且在创建DOM之后运行脚本。

我修改了你的剧本,所以你可以有更好的理解。

  • 在输入上设置一个ID,这样我们就可以从脚本中锁定该输入。
  • 在正文的结束标记之前编写脚本,这样脚本就可以在创建所有DOM元素之后运行。

null

<!DOCTYPE html>
   <html>
    <head>
        <meta charset="utf-8">
        <title>Titel</title>
    </head>
    <body>
                 

       <a href="https://translate.google.com/">
          <img class="logo-search" src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" style="position: absolute; left:45%" width="10%" height="10%">
       </a>
       <form  class="search-form" method="get" action="https://translate.google.com/" target="_top"; style="position: absolute; left:250pt; top: 30%">
          <input id="search-box" type="text" name="q" class="search-bar" style="font-size: 30pt; width: 150%; font-family: 'Times New Roman', Times, serif;">
       </form>
    
       <script>
         var datum = new Date(); // aktuální datum
         var retezec = ""; // postupně se k němu budou přičítat další řetězce
         retezec += datum.getDate() + ". "; // Den v měsíci
         retezec += (1 + datum.getMonth()) + ". "; // Měsíce jsou číslovány od nuly
         document.getElementById('search-box').value = retezec;
       </script>
     </body>