2016-08-30 3 views
-1

Mein JavaScript funktioniert nicht, wenn ich es in Asps Webseite in einem Skript-Tag. Code ist in den Bildern und im Code auch.JavaScript funktioniert nicht in aspx C# Webseite

Es ist eigentlich ein Modell Box Bilder wie lightBox/fancyBox zu öffnen, aber nur das Modell Box für 1 Sekunde geöffnet, aber es funktioniert in normaler HTML-Seite

ASP and HTML code

JavaScript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="img-model.aspx.cs" >Inherits="webpages_img_model" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>model box</title> 
<style> 
     /* The Modal (background) */ 
     .modal { 
      display: none; /* Hidden by default */ 
      position: fixed; /* Stay in place */ 
      z-index: 1; /* Sit on top */ 
      padding-top: 100px; /* Location of the box */ 
      left: 0; 
      top: 0; 
      width: 100%; /* Full width */ 
      height: 100%; /* Full height */ 
      overflow: auto; /* Enable scroll if needed */ 
      background-color: rgb(0,0,0); /* Fallback color */ 
      background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
     } 

     /* Modal Content */ 
     .modal-content { 
      background-color: #fefefe; 
      margin: auto; 
      padding: 20px; 
      border: 1px solid #888; 
      width: 60%; 
      height:auto; 
     } 

     /* The Close Button */ 
     .close { 
      color: #aaaaaa; 
      float: right; 
      font-size: 28px; 
      font-weight: bold; 
     } 
     .clear { 
      clear:both; 
     } 

      .close:hover, 
      .close:focus { 
       color: #000; 
       text-decoration: none; 
       cursor: pointer; 
      } 
      .modal-content .p1 { 
       float:left; 
       border-right:1px; 
       width:50%; 
      } 
      .modal-content .p2 { 
       float:right; 
       width:50%; 

      } 
      #img { 
      position:relative; 
      width:400px; 
      height:400px; 

     } 
    #img #mm{ 
      position:absolute; 
      top:0px; 
      right:0px; 
      z-index: 16; 
     } 
     #img img{ 
      width:100%; 
      height:auto; 
     } 
    </style> 


</head> 
<body> 

    <form id="form1" runat="server"> 

      <h2>Modal Example</h2> 
    <div id="img"> 
     <a href=""> 
      <img src="../images/1pic.jpg" width="400px" height="400px" /> 
      <span id="mm"><button id="d">MM</button> </span> 
     </a> 
    </div> 
    <!-- Trigger/Open The Modal --> 
    <button id="myBtn">Open Modal</button> 

    <!-- The Modal --> 
    <div id="myModal" class="modal"> 

     <!-- Modal content --> 
     <div class="modal-content"> 
      <span class="close">×</span> 

         <div class="p1" > 
          <img src="../images/1pic.jpg" height="100%" width="80%" > /> 
        </div> 

         <div class="p2"> 
       <p>Some text in the Modal..</p> <br /> 
     <p>Some text in the Modal..</p> <br /> 
     <p>Some text ithe Modal..</p> <br /> 
     <p>Some text in the Modal..</p> <br /> 
      <input id="Button1" type="button" value="button" /> 
         </div> 
      <div class="clear"></div> 
     </div> 

    </div> 
      <script type="text/javascript"> 
       var modal = document.getElementById('myModal'); 

       // Get the button that opens the modal 
       var btn = document.getElementById("myBtn"); 

       // Get the <span> element that closes the modal 
       var span = document.getElementsByClassName("close")[0]; 

       // When the user clicks the button, open the modal 
       btn.onclick = function() { 
       modal.style.display = "block"; 
       } 

       // When the user clicks on <span> (x), close the modal 
       span.onclick = function() { 
        modal.style.display = "none"; 
       } 

       // When the user clicks anywhere outside of the modal, close it 
      $window.onclick = function (event) { 
        if (event.target == modal) { 
         modal.style.display = "none"; 
        } 
       } 
     </script> 



     </form> 
</body> 
</html> 
+0

wie Pang sagt, geben Sie uns Code, nicht Screenshots. Sie haben uns auch nicht gesagt, was nicht funktioniert? Hast du Fehler? Tut es nicht etwas, was es tun soll? Was soll es überhaupt tun? Das ist im Moment eine so schlechte Frage. – DavidG

+0

Sorry, ich habe versucht, aber es gab mir Fehler. Ich verbrachte 20 Minuten damit und entschied dann, Bilder –

+0

in Ordnung zu setzen, sollte ich Code in Anführungszeichen setzen. um ehrlich zu sein, ich versuche es wieder zu tun, aber ich kann nicht herausfinden wie. Ich drücke Code-Taste und dann den Code einfügen und es zeigt wieder denselben Fehler –

Antwort

0

Als ich Ihr Problem kommentierte, wenn der Benutzer auf die Schaltfläche "Modal öffnen" klickt, wird das Formular übermittelt, was bedeutet, dass die Seite im Wesentlichen neu geladen wurde. Was Sie tun müssen, ist false auf btn.onclick = function() Funktion zurückgeben. Der Einfachheit halber habe ich den gesamten Code geschrieben:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="img-model.aspx.cs" >Inherits="webpages_img_model" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>model box</title> 
    <style> 
     /* The Modal (background) */ 
     .modal { 
      display: none; /* Hidden by default */ 
      position: fixed; /* Stay in place */ 
      z-index: 1; /* Sit on top */ 
      padding-top: 100px; /* Location of the box */ 
      left: 0; 
      top: 0; 
      width: 100%; /* Full width */ 
      height: 100%; /* Full height */ 
      overflow: auto; /* Enable scroll if needed */ 
      background-color: rgb(0,0,0); /* Fallback color */ 
      background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
     } 

     /* Modal Content */ 
     .modal-content { 
      background-color: #fefefe; 
      margin: auto; 
      padding: 20px; 
      border: 1px solid #888; 
      width: 60%; 
      height:auto; 
     } 

     /* The Close Button */ 
     .close { 
      color: #aaaaaa; 
      float: right; 
      font-size: 28px; 
      font-weight: bold; 
     } 
     .clear { 
      clear:both; 
     } 

     .close:hover, 
     .close:focus { 
      color: #000; 
      text-decoration: none; 
      cursor: pointer; 
     } 
     .modal-content .p1 { 
      float:left; 
      border-right:1px; 
      width:50%; 
     } 
     .modal-content .p2 { 
      float:right; 
      width:50%; 
     } 
     #img { 
      position:relative; 
      width:400px; 
      height:400px; 
     } 
     #img #mm{ 
      position:absolute; 
      top:0px; 
      right:0px; 
      z-index: 16; 
     } 
     #img img{ 
      width:100%; 
      height:auto; 
     } 
    </style> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <h2>Modal Example</h2> 
     <div id="img"> 
      <!-- added # --> 
      <a href="#"> 
       <!-- I would set the dimensions width and height in CSS --> 
       <img src="../images/1pic.jpg" width="400" height="400" /> 
       <span id="mm"> 
        <button id="d">MM</button> </span> 
      </a> 
     </div> 
     <!-- Trigger/Open The Modal --> 
     <button id="myBtn">Open Modal</button> 

     <!-- The Modal --> 
     <div id="myModal" class="modal"> 
      <!-- Modal content --> 
      <div class="modal-content"> 
       <span class="close">×</span> 
       <div class="p1" > 
        <img src="../images/1pic.jpg" height="100" width="80" /> 
       </div> 
       <div class="p2"> 
        <p>Some text in the Modal..</p> <br /> 
        <p>Some text in the Modal..</p> <br /> 
        <p>Some text ithe Modal..</p> <br /> 
        <p>Some text in the Modal..</p> <br /> 
        <input id="Button1" type="button" value="button" /> 
       </div> 
       <div class="clear"> 
        &nbsp; 
       </div> 
      </div> 
     </div> 

     <script type="text/javascript"> 
      var modal = document.getElementById('myModal'); 

      // Get the button that opens the modal 
      var btn = document.getElementById("myBtn"); 

      // Get the <span> element that closes the modal 
      var span = document.getElementsByClassName("close")[0]; 

      // When the user clicks the button, open the modal 
      btn.onclick = function() { 
       modal.style.display = "block"; 
       /* added to prevent the page to submitting */ 
       return false; 
      } 

      // When the user clicks on <span> (x), close the modal 
      span.onclick = function() { 
       modal.style.display = "none"; 
      } 

      // When the user clicks anywhere outside of the modal, close it 
      /* got rid of the $window and just replaced it with window */ 
      window.onclick = function (event) { 
       if (event.target == modal) { 
        modal.style.display = "none"; 
       } 
      } 
     </script> 
    </form> 
</body> 
</html> 

ich einige andere Änderungen auch auf die HTML und JavaScript gemacht, siehe Inline-Kommentare

+0

danke sir, du hast mein problem gelöst. Ich schätze deine Hilfe sehr. Vielen Dank –

Verwandte Themen