[英]Convert this into EXTERNAL JAVASCRIPT FILE
They actually have an inline javascript code and I am trying to convert it to an external javascript file.他们实际上有一个内联 javascript 代码,我正在尝试将其转换为外部 javascript 文件。 I want to get rid of my onclicks but I dont know how to rewrite my JS to be external.我想摆脱我的点击,但我不知道如何将我的 JS 重写为外部。 Here's the code I'm working at.这是我正在使用的代码。
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<br>
<div style="text-align:center">
<span class="dot" onclick="currentSlide(1)"></span>
<span class="dot" onclick="currentSlide(2)"></span>
<span class="dot" onclick="currentSlide(3)"></span>
<span class="dot" onclick="currentSlide(4)"></span>
</div>
<script> let slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { let i; const slides = document.getElementsByClassName("mySlides"); const dots = document.getElementsByClassName("dot"); if (n > slides.length) {slideIndex = 1} if (n < 1) {slideIndex = slides.length} for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex-1].style.display = "block"; dots[slideIndex-1].className += " active"; } </script>
Simply, create js
file.很简单,创建js
文件。 Like, script.js
.比如, script.js
。 Then, add your javascrript code inside the script.js
.然后,在script.js
中添加您的 javascript 代码。 In script.js :在script.js中:
let slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
let i;
const slides = document.getElementsByClassName("mySlides");
const dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
Now, add the external file to the html file.现在,将外部文件添加到 html 文件中。 Like:喜欢:
<script src="script.js"></script>
You, can add this linking line in the head or at the bottom of body.您可以在头部或身体底部添加此链接线。
*Note:- If you add at the top ie, head, then script will load first before the document inside body. *注意:- 如果您在顶部添加即头部,则脚本将在正文内的文档之前首先加载。 Else, it will load after on.否则,它将在打开后加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.