如何在 html 页面中调用 jquery


How to call jquery in a html page?

    var currentIndex = 0,
      items = $('.container div'),
      itemAmt = items.length;
    function cycleItems() {
      var item = $('.container div').eq(currentIndex);
      items.hide();
      item.css('display','inline-block');
    }
    var autoSlide = setInterval(function() {
      currentIndex += 1;
      if (currentIndex > itemAmt - 1) {
        currentIndex = 0;
      }
      cycleItems();
    }, 3000);
    $('.next').click(function() {
      clearInterval(autoSlide);
      currentIndex += 1;
      if (currentIndex > itemAmt - 1) {
        currentIndex = 0;
      }
      cycleItems();
    });
    $('.prev').click(function() {
      clearInterval(autoSlide);
      currentIndex -= 1;
      if (currentIndex < 0) {
        currentIndex = itemAmt - 1;
      }
      cycleItems();
    });

这是我在另一个文件中的jquery。您知道如何从我的 html 页面包含此脚本吗?

<button class="next">Next</button>
  <button class="prev">Previous</button>
  <div class="container">
    <div style="display: inline-block;">
      <img src="http://placeimg.com/400/200/people"/>
    </div>
    <div>
     <img src="http://placeimg.com/400/200/any"/>
    </div>
    <div>
      <img src="http://placeimg.com/400/200/nature"/>
    </div>
    <div>
      <img src="http://placeimg.com/400/200/architecture"/>
    </div>
    <div>
      <img src="http://placeimg.com/400/200/animals"/>
    </div>
    <div>
      <img src="http://placeimg.com/400/200/people"/>
    </div>
    <div>
      <img src="http://placeimg.com/400/200/tech"/>
    </div>
  </div>

这是我的 html 文件,我在代码的开头包含了脚本。但似乎没有建立连接?我是这方面的初学者。谢谢

如果你想

在你的php文件中保留javascript代码,那么把它包装成<script> </script>标签,放在php文件的末尾

如果它是一个单独的.js文件,则将其包含在 php 文件的末尾<script type="text/javascript" src="Path/of/your/.js/file"> </script> .

你可以

把所有的jquery放在.js文件中,假设你把你的jquery放在custom.js文件中:

在你的 html/php 文件中,在开头包含这个自定义.js文件:

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

您可以使用 HTML 的脚本标签将整个 jquery 包含在 PHP 页面上。首先复制代码并将其放在脚本标签中。请看下面。

<script type="text/javascript"> 
// your jquery code // 
</script>