2016-05-17 3 views
1
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ページから取り込む方法を知っていますか? htmlページでjqueryを呼び出す方法は?

<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ファイルであると私は、コードの先頭にスクリプトが含まれています。しかし、接続が作成されていないようですか?私はこれの初心者です。おかげ

+0

「スクリプトタグ」を使用するだけで – Nehal

答えて

-1

あなたは、あなたのPHPファイル内のJavaScriptコードを保つ<script> </script>タグにそれをラップし、あなたのPHPファイルの終わりにそれを入れたい場合。

.jsファイルの場合は、ファイルの末尾に<script type="text/javascript" src="Path/of/your/.js/file"> </script>を含めてください。

+0

ありがとうの背後に正しい理由のように見えることができません。私はちょうどそれを試みた。私はPHPファイルの前に置いてはいけません。なぜなら、私のクラスはすべて見つからないからです。 –

-1

あなたは、.jsファイル内のすべてのjQueryを入れて、あなたがcustom.jsファイルであなたのjQueryを入れていると仮定することができます

あなたのhtml/phpのファイルでは、これが最初にファイルをcustom.js含めます

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

理由を言及せずに投票していますか? –

+1

まさに!!!! downvote –

-1

HTMLのscriptタグを使用してPHPページにjquery全体を含めることができます。まず、コードをコピーしてscriptタグ内に置きます。以下をご覧ください。

<script type="text/javascript"> 

// your jquery code // 

</script> 
関連する問題