微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在hbs文件内的script标记中延迟属性

如何解决在hbs文件内的script标记中延迟属性

我正在使用express在Java脚本中编写代码我有一个类似于html文件的index.hbs文件。我尝试将代码写在script标签

<div class="container" style="display: flex; 
  flex-direction:column;
  align-items: center;
  justify-content: center; 
  font-family: 'Gill Sans','Gill Sans MT',Calibri,'Trebuchet MS',sans-serif;">
  <h1 style="color:pink">{{title}}</h1>
  <p style="color:tomato">Welcome to {{title}}</p>
  <div style="display: flex;
  align-items: center;
  justify-content: center">
    <form method="post" action="/urls">
      <label for="long">Enter the URL to Shorten</label>
      <br>
      <br>
      <input style="width: 400px" id="long" name="LongURL" placeholder="longUrl" />
      <input type="submit" value="Отправить">
    </form>
  </div>
  <h1 style="color:pink">Shortened URL's</h1>
  <ul>
    {{#each db}}
    <li style="width:100%">
      <p>{{this.longId}}</p>
      <a href="{{this.longId}}" target="_blank">
        <p>{{this.shortId}}</p>
      </a>
      <p>Number Visited: {{this.numberUsed}}</p>
    </li>
    {{/each}}
  </ul>
</div>

查找带有标签a的元素。我会写

<script>
  console.log(document.querySelector("a"));
</script>

在html文件的末尾,但我想使用defer。如果我在index.hbs内的HTML代码之前的script标签中写入此属性,它将返回空值。我该怎么办?

解决方法

// Try this way 
window.onload = function (){
    console.log(document.querySelector('a'))
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。