简体   繁体   English

如何将carousel()外部javascript函数调用为html文件?

[英]how can i call carousel() an external javascript function into html file?

How can i call external java script file's function 我如何调用外部Java脚本文件的功能

  • carousel() 轮播()

    in HTML file? 在HTML文件中? This is an automatic slideshow of pictures. 这是图片的自动幻灯片显示。 how this java script function call into HTML file, so the slideshow display and run automatically? 这个Java脚本函数如何调用HTML文件,以便幻灯片显示并自动运行? also pictures change after a short time . 短时间后图片也会改变。

 var slideIndex = 0; carousel(); function carousel() { var i; var x = document.getElementsByClassName("slides"); for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } slideIndex++; if (slideIndex > x.length) {slideIndex = 1} x[slideIndex-1].style.display = "block"; setTimeout(carousel, 2000); // Change image every 2 seconds } 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>PUCIT</title> <script type="text/javascript" src="js.js"></script> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div id="slideshow" > <img class="slides" src="pic1.jpg"> <img class="slides" src="pic3.jpg"> <img class="slides" src="pic4.jpg"> <img class="slides" src="pic5.jpg"> <img class="slides" src="pic2.jpg"> </div> </body> </html> 

I assume js.js is the file in question , which contain the carousel(). 我假设js.js是有问题的文件,其中包含carousel()。

The way you include the file into the HTML: 将文件包含到HTML中的方式:

<script type="text/javascript" src="js.js"></script>

means it have to be in the same folder as the html file. 表示它必须与html文件位于同一文件夹中。

The only thing you have to do to execute the function is to call it. 要执行该功能,唯一要做的就是调用它。 Put this code before closing tag: 将这段代码放在结束标记之前:

<script type="text/javascript">
    carousel();
</script>

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

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