2017-07-07 10 views
-1

私のウェブページはページの読み込み時にスライドショーを表示する必要がありますが、スクリプトの機能は動作していないようです。私はprompt()とconfirm()関数を含む単純なスクリプトを実行しようとしましたが、うまくいきましたが、カスタム関数は動作していないようです。同じように私の "modal.js"スクリプトは、ボタンクリック時にモーダルボックスを表示する必要があります。助けてくださいJavaScriptの機能が実装されていません

<!doctype html> 
<html lang = "en"> 
<head> 
    <title> Ice Cream </title> 
    <link rel="stylesheet" href="main_ice.css" /> 
    <script type = "text/javascript" src = "slideShow.js"> </script> 
    <script src = "modal.js"> </script> 
    <meta charset = "utf-8" /> 
</head> 
<body> 
    <div id = "big_wrapper"> 
      <header class= "top_header"> 
       <hgroup> 
        <h1> ICE Funky </h1> 
        <h3> ice cream production </h3> 
       </hgroup> 
      </header> 

      <nav class= "nav_bar"> 
       <ul> 
        <label><li name = "home"> Home </li> 
        <li> About Us </li> 
        <li> Products </li> 
        <li> Services </li> 
        <li> Contacts </li> 
        </label> 
       </ul> 
      </nav> 


     <div id = "slideshow"> 

      <span class = "image_slide"><img src = "4072.jpg" width = "500px" height = "300px"/> </span> 
      <span class = "image_slide"><img src = "26551.jpg" width = "500px" height = "300px"/> </span> 
      <span class = "image_slide"><img src = "30225.jpg" width = "500px" height = "300px"/> </span> 
      <span class = "image_slide"><img src = "74223.jpg" width = "500px" height = "300px"/> </span> 
      <span class = "image_slide"><img src = "74285.jpg" width = "500px" height = "300px"/> </span> 



     </div> 
     <button id = "modalButton"> Modal </button> 
       <div id = "myModal"> 
        <div id = "modal_title"> Main Title </div><br><br> 
        <div id = "modal_body"> Body </div> 
       </div> 

    </div> 

</body> 
</html> 

!----------- CSSファイル----------------!

*{ 
    margin:0px; 
    padding:0px; 
} 

header, footer, nav, hgroup, aside, article, section{ 
    display : block; 
} 

body{ 
    width:100%; 
    display: -webkit-box; 
    -webkit-box-pack: center; 
} 

#big_wrapper{ 
    max-width: 100%; 
    display: -webkit-box; 
    -webkit-box-orient: vertical; 
    -webkit-box-flex: 1; 
} 

.top_header{ 
    position: absolute; 
    margin: 20px 0px 0px 120px; 
    border: 2px solid black; 
    width: 180px; 
    padding: 25px; 

} 

.nav_bar{ 
    margin-left: 350px; 
    margin-top: 85px; 
    text-align: center; 
} 
.nav_bar li{ 
    position: relative; 
    list-style: none; 
    display: inline-block; 
    -webkit-box-flex: 1; 
    margin: 20px; 
    border: 2px solid white; 
    padding: 5px; 
    -webkit-transition: border-left 1s, border-top 3s, border-right 4s, 
         border-bottom 6s; 
} 

.nav_bar li:hover{ 
    border-left: 2px solid black; 
    border-top: 2px solid black; 
    border-right: 2px solid black; 
    border-bottom: 2px solid black; 
} 

#slideshow{ 
    position: absolute; 
    margin-top: 50px; 
    margin-left: 400px; 
    width: 500px; 
} 

.image_slide{ 
    position: absolute; 
    /*display: none;*/ 


} 

.prev, .next { 
    cursor: pointer; 
    position: absolute; 
    top: 150px; 
    width: auto; 
    margin-top: -22px; 
    padding: 16px; 
    color: yellow; 
    font-weight: bold; 
    font-size: 18px; 
    transition: 0.6s ease; 
    border-radius: 0 3px 3px 0; 
} 

.next{ 
    /*left: 458px;*/ 
    right: 0px; 
} 

.prev:hover, .next:hover { 
    background-color: rgba(0,0,0,0.8); 
} 

#modalButton{ 
    background: rgba(0,256,0,0.5); 
    padding: 5px; 
    margin-left: 40px; 
    margin-top: 40px; 
    color: chocolate; 
} 

#myModal{ 
    position: fixed; 
    top:0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    z-index: 0; 
    background-color: rgba(0,0,0,0.4); 
    display: none; 
} 

#modal_title{ 
    width: 95%; 
    height: 5%; 
    position: fixed; 
    bottom: 15%; 
    left: 30px; 
    background: rgba(0,256,0,0.4); 
} 

#modal_body{ 
    width: 95%; 
    height: 10%; 
    position: fixed; 
    top: 85%; 
    left: 30px; 
    background: rgba(256,256,256,0.4); 

} 

!-------------- JSファイル(slideShow.js)----------------!

var slideIndex = 0; 
    showSlides(); 

    function showSlides() { 
     var i; 
     var slides = document.getElementsByClassName("image_slide"); 
     for (i = 0; i < slides.length; i++) { 
      slides[i].style.display = "none"; 
     } 
     slideIndex++; 
     if (slideIndex> slides.length) {slideIndex = 1} 
     slides[slideIndex-1].style.display = "block"; 
     setTimeout(showSlides, 2000); // Change image every 2 seconds 
    } 

!--------------- JSファイル(modal.js)------------------!

modalButton = getElementById("modalButton"); 
myModal = getElementById("myModal"); 
modalButton.onclick = function(){ 
    prompt("hi"); 
    myModal.style.display = "block"; 

} 
+1

コンソールにエラーがありますか? – Lazyexpert

+2

'getElementById(" modalButton ")の代わりに' document.getElementById( "modalButton") 'を使います。 – ricky

答えて

0

あなたはgetElementById()を直接使用しているのではなく、document.getElementById()を使用する必要があります。

modal.jsファイルを変更します。

modalButton = document.getElementById("modalButton"); 
myModal = document.getElementById("myModal"); 
modalButton.onclick = function(){ 
    prompt("hi"); 
    myModal.style.display = "block"; 

} 

の作業フィドル - https://jsfiddle.net/arjunskumar47/L8Lvwywe/

1

まず、リッキーで述べたように、あなたはdocument.getElementById("modalButton")の代わりgetElementById("modalButton")を使用する必要があります。

第2に、マークアップが解析される前にスクリプトが評価されます。 bodyタグの最後にあなたのheadのうち、スクリプトタグを移動するか、あるいは負荷のイベントリスナーの両方のファイルの内容をラップし、次のいずれか

window.addEventListener('load', function() { 
    // your code here 
}); 

modal.jsについては、次のようになります。

window.addEventListener('load', function() { 
    var modalButton = document.getElementById("modalButton"); 
    var myModal = document.getElementById("myModal"); 
    modalButton.onclick = function(){ 
     prompt("hi"); 
     myModal.style.display = "block"; 
    } 
}); 
関連する問題