问题描述
我正在使用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'))
}